Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any character including newline - Java Regex

Tags:

java

regex

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

like image 944
Mick Avatar asked Jul 11 '10 10:07

Mick


People also ask

How do you match a character including newline in regex?

By default in most regex engines, . doesn't match newline characters, so the matching stops at the end of each logical line. If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.

What does \\ mean in Java regex?

String regex = "\\."; Notice that the regular expression String contains two backslashes after each other, and then a . . The reason is, that first the Java compiler interprets the two \\ characters as an escaped Java String character. After the Java compiler is done, only one \ is left, as \\ means the character \ .

What is line terminator regex?

Line terminators A carriage-return character followed immediately by a newline character ("\r\n"), A standalone carriage-return character ('\r'), A next-line character ('\u0085'), A line-separator character ('\u2028'), or. A paragraph-separator character ('\u2029).

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.


2 Answers

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].

like image 97
Artefacto Avatar answered Oct 19 '22 03:10

Artefacto


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)

like image 43
Jason L. Avatar answered Oct 19 '22 04:10

Jason L.