tags:

views:

51

answers:

1

I thought it may be [.\n]+ but that doesn't seem to work?

+2  A: 

The dot cannot be used inside character classes.

See the option Pattern.DOTALL.

Pattern.DOTALL Enables dotall mode. In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators. Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)

If you need it on just a portion of the regular expression, you use e.g. [\s\S].

Artefacto
Arguably cleaner than `[\s\S]` would be `(?:.|\n)`. The reason that `[.\n]` doesn't work is that `.` isn't special in character classes; specifying the same thing with a literal or, `|`, works fine.
Antal S-Z
@Antal I don't think `(?:.|\n)` is as portable since a new line in Windows is `\r\n`. Maybe `(?:.|\n|\r)`, though now the `\r` is redundant in Unix.
Artefacto
@Artefacto: Good call. I think the usual solution is `\r?\n`.
Antal S-Z
In Java, just do `(?s:`...`)` to enable DOTALL mode for a specific section, and stop worrying about stupid OSs.
Peter Boughton
Or, of course, `(?s)`...`(?-s)` to toggle it on then off at those points.
Peter Boughton