Any character including newline - Java Regex

JavaRegex

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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)

Hints:
\s: any whitespace character
like (space/newLine/...)

\S: any non-whitespace character
like (characters /sepcial characters/ numbers / ...)

Solution 3 - Java

Try this

((.|\n)*)

It matches all characters multiple times

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMickView Question on Stackoverflow
Solution 1 - JavaArtefactoView Answer on Stackoverflow
Solution 2 - JavaJason L.View Answer on Stackoverflow
Solution 3 - JavaYoungkhafView Answer on Stackoverflow