I thought it may be [.\n]+
but that doesn't seem to work?
Asked
Active
Viewed 8.7k times
3 Answers
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
-
2Arguably 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
-
1Good call. I think the usual solution is `\r?\n`. – Antal Spector-Zabusky Jul 11 '10 at 12:33
-
14In 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
-
5Or, 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
-
5this appears to be very expensive, `[\s\S]` appears to be a lot kinder on the regex engine anecdotally. – ThorSummoner May 06 '16 at 19:57
1
-
This cause java.lang.StackOverflowError in java. Is there any workaround in java? – SudoCoder Jul 22 '21 at 08:03
-
1