From this API:
MULTILINE
public static final int 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)
.
Can anybody make a real life code example of the difference of having a Pattern created with Pattern.MULTILINE
and with standard settings?
The boundary matcher ^
as default should match the beginning of the line and $
the end of the line as this tutorial explaines.
What does it change by using the Pattern.MULTILINE
?
Pattern. MULTILINE or (? m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string).
Multiline option, it matches either the newline character ( \n ) or the end of the input string.
Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .
Contrived example: you want to match a specific import line in a Java source file, say:
import foo.bar.Baz;
In order to match that line anywhere in the input, which is multiline, the easier solution is to use Pattern.MULTILINE
along with this regex:
^\s*import\s+foo\.bar\.Baz\s*;\s*$
Here the ^
will match right after a newline and the $
right before. Which is desirable in such a situation.
And:
The boundary matcher ^ as default should match the beginning of the line and $ the end of the line as this tutorial explaines.
this is untrue. By default, ^
matches the beginning of the input, and $
matches the end of the input.
Illustration:
public static void main(final String... args)
{
final Pattern p1 = Pattern.compile("^dog$");
final Pattern p2 = Pattern.compile("^dog$", Pattern.MULTILINE);
final String input = "cat\ndog\nTasmanian devil";
System.out.println(p1.matcher(input).find());
System.out.println(p2.matcher(input).find());
}
This outputs:
false
true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With