Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pattern.MULTILINE in java regex

Tags:

java

regex

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?

like image 458
Rollerball Avatar asked Jul 11 '13 11:07

Rollerball


People also ask

What is pattern multiline Java?

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

What is multiline mode in regex?

Multiline option, it matches either the newline character ( \n ) or the end of the input string.

What does \\ mean in Java regex?

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.

How do I match a pattern in 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" .


1 Answers

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
like image 189
fge Avatar answered Oct 03 '22 14:10

fge