Rollerball
Rollerball

Reputation: 13118

Escape sequence in regex parsed by Pattern.LITERAL

Given the following snippet:

Pattern pt = Pattern.compile("\ndog", Pattern.LITERAL);
Matcher mc = pt.matcher("\ndogDoG");
while(mc.find())
{

    System.out.printf("I have found %s starting at the " +
            "index %s and ending at the index    %s%n",mc.group(),mc.start(),mc.end());

}

The output will be:

I have found 
dog starting at the index 0 and ending at the index 4.

It means that even though I have specified Pattern.LITERAL which this link says that:

Pattern.LITERAL Enables literal parsing of the pattern. When this flag is specified then the input string that specifies the pattern is treated as a sequence of literal characters. Metacharacters or escape sequences in the input sequence will be given no special meaning.

However the output given from the above snippet does interpret the \n escape sequence, it does not treat it like a literal.

Why does it happen in that way since they specify in this tutorial that it should not?

I now \n is a line terminator, however it's still an escape sequence character.

Upvotes: 1

Views: 998

Answers (2)

fge
fge

Reputation: 121860

however it's still an escape sequence character.

No it's not. It's a newline character. You can do:

char c = '\n';

Your output is therefore expected.

Note that if you compile a pattern with:

Pattern.compile("\n")

then \n is the literal character \n.

BUT if you compile with:

Pattern.compile("\\n")

then it is an escape sequence. And they happen to match the same thing.

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336498

Pattern.LITERAL cares about regex literals, not string literals.

Therefore, it treats \\n as backslash plus n (instead of the regex token for newline), but it treats \n as the line feed character that it stands for (and thus ignores it).

Upvotes: 1

Related Questions