73

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

buhtz
  • 10,774
  • 18
  • 76
  • 149
Mick
  • 733
  • 1
  • 5
  • 4

3 Answers3

80

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
  • 96,375
  • 17
  • 202
  • 225
  • 2
    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 Spector-Zabusky Jul 11 '10 at 12:09
  • @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 Jul 11 '10 at 12:12
  • 1
    Good call. I think the usual solution is `\r?\n`. – Antal Spector-Zabusky Jul 11 '10 at 12:33
  • 14
    In Java, just do `(?s:`...`)` to enable DOTALL mode for a specific section, and stop worrying about stupid OSs. – Peter Boughton Jul 11 '10 at 12:58
  • 5
    Or, of course, `(?s)`...`(?-s)` to toggle it on then off at those points. – Peter Boughton Jul 11 '10 at 13:01
  • New URI for Pattern.DOTALL documentation: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html – Steve HHH May 21 '13 at 20:29
  • Strictly speaking, dot `.` can be used inside character classes, it just doesn't have a special meaning, it just matches a dot. – neuralmer May 12 '16 at 14:15
  • Also, use of `[\s\S]` on a CR-LF terminated file could match just one half of a line ending. Perhaps, `(?:.|\r\n|\r|\n)` would be more precise. – neuralmer May 12 '16 at 14:21
55

Edit: While my original answer is technically correct, as ThorSummoner pointed out, it can be done more efficiently like so

[\s\S]

as compared to (.|\n) or (.|\n|\r)

Jason L.
  • 1,125
  • 11
  • 19
1

Try this

((.|\n)*)

It matches all characters multiple times

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
Youngkhaf
  • 11
  • 1