Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using Pattern.LITERAL mean the same as Pattern.quote?

Tags:

java

regex

More precisely: Do

Pattern.compile(s, x | Pattern.LITERAL)

and

Pattern.compile(Pattern.quote(s), x)

create equivalent regexes for any String s and any other flag x?

If not, how to simulate Pattern.LITERAL?

like image 756
maaartinus Avatar asked Jan 29 '11 22:01

maaartinus


2 Answers

Short answer: For your example, yes.

Long answer: Yes, but Pattern.quote is more flexible. What if you only wanted some of your pattern quoted? Like:

Pattern.compile(Pattern.quote(s) + "+", x)

With setting the Pattern.LITERAL flag, even the + character would now be treated literally.

If you don't trust the documentation, maybe looking at the source code at Google Code Search for Pattern.compile will help.

From what I can derive from looking at the source code:

  • If the LITERAL flag is not set, regardless of all the other flags, it will look for any \Q...\E quoted blocks and manually escape special characters, just as one would expect.

  • If the LITERAL flag is set, it will convert the whole pattern using the newSlice method, and there are special cases for at least the CASE_INSENSITIVE flag and UNICODE_CASE flag

like image 94
Zach L Avatar answered Oct 09 '22 03:10

Zach L


Given the question as is, the answer is no, because of setting x=Pattern.LITERAL leading to quoting s twice in the second expression. With double quoting and s="A" the String "A" won't be matched, but the String "\\QA\\E" will. However,

Pattern.compile(s, x | Pattern.LITERAL)

seem to be equivalent to

Pattern.compile(Pattern.quote(s), x & ~Pattern.LITERAL)
like image 28
maaartinus Avatar answered Oct 09 '22 02:10

maaartinus