Any character including newline - Java Regex
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]
.
Java Regex is including new line in match
Try using the Pattern.MULTILINE option
Pattern rgx = Pattern.compile("^(\\S+)$", Pattern.MULTILINE);
This causes the regex to recognise line delimiters in your string, otherwise ^
and $
just match the start and end of the string.
Although it makes no difference for this pattern, the Matcher.group()
method returns the entire match, whereas the Matcher.group(int)
method returns the match of the particular capture group (...)
based on the number you specify. Your pattern specifies one capture group which is what you want captured. If you'd included \s
in your Pattern as you wrote you tried, then Matcher.group()
would have included that whitespace in its return value.
How do I match any character across multiple lines in a regular expression?
It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:
/(.*)<FooBar>/s
The s at the end causes the dot to match all characters including newlines.
In Java regex how to match newline character
I believe the issue is that the Pattern.MULTILNE
is incorrect. For the particular example, it should be Pattern.DOTALL
(or embed the ?s in the expression).
MULTILINE:
Enables multiline mode.
In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.
Multiline mode can also be enabled via the embedded flag expression (?m).
DOTALL:
In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
A working example using DOTALL
Java Regex does not match newline
UPDATE:
Based on your comments, you need something like this:
.*(?:[ \r\n\t].*)+
EXPLANATION:
In plain words, it is a regex that matches a line, then 1 or more lines. Or, just a multiline text.
.*
- 0 or more characters other than a newline(?:[ \r\n\t].*)+
- a non-capturing group that matches 1 or more times a sequence of[ \r\n\t]
- either a space, or a\r
or\n
or\t
.*
- 0 or more characters other than a newline
See demo
Original answer
You can fix your pattern 2 ways:
String REGEX = ".*(?:\r\n|[ \t\r\n]).*";
This way we match either \r\n
sequence, or any character in the character class.
Or (since the character class only matches 1 character, we can add +
after it to capture 1 or more:
String REGEX = ".*[ \t\r\n]+.*";
See IDEONE demo
Note that it is not a good idea to use single characters in alternations, it decreases performance.
Also note that capturing groups should not be overused. If you do not plan to use the contents of the group, use non-capturing groups ((?:...)
), or remove them.
Regex to match any character including new lines
Add the s
modifier to your regex to cause .
to match newlines:
$string =~ /(START)(.+?)(END)/s;
How to match any character in regular expression?
Yes, you can. That should work.
.
= any char except newline\.
= the actual dot character.?
=.{0,1}
= match any char except newline zero or one times.*
=.{0,}
= match any char except newline zero or more times.+
=.{1,}
= match any char except newline one or more times
java new line regular expression
By default . matches every character except new line character.
In this case, you will need DOTALL option, which will make . matches any character, including new line character. DOTALL option can be specified inline as (?s). For example:
(?s)<ABC>((.|\\n)+?)</ABC>
Related Topics
How to Load Image File to Imageview
Why Can't I Use \U000D and \U000A as Cr and Lf in Java
Jackson Change JSONignore Dynamically
Often Big Numbers Become Negative
Post Increment Operator Not Incrementing in for Loop
No Idea Annotations Attached to the Jdk 1.8 (C:\...), Some Issues Will Not Be Found
How to Include External Jar on My Netbeans Project
Java Swingworker Thread to Update Main Gui
How to Calculate the Elapsed Time of an Event in Java
Java Try-Finally Return Design Question
Using PDFbox to Write Utf-8 Encoded Strings to a PDF
Parsing String to Local Date Doesn't Use Desired Century
How to Inject Module Declaration into Jar