Reputation: 31248
I am having difficulty with using \b
as a word delimiter in Java Regex.
For
text = "/* sql statement */ INSERT INTO someTable";
Pattern.compile("(?i)\binsert\b");
no match found
Pattern insPtrn = Pattern.compile("\bINSERT\b");
no match found
but
Pattern insPtrn = Pattern.compile("INSERT");
finds a match
Any idea what I am doing wrong?
Upvotes: 2
Views: 259
Reputation: 213261
Use this instead: -
Pattern insPtrn = Pattern.compile("\\bINSERT\\b")
You need to escape \b
with an extra backslash..
Upvotes: 2
Reputation: 208485
When writing regular expressions in Java, you need to be sure to escape all of the backslashes, so the regex \bINSERT\b
becomes "\\bINSERT\\b"
as a Java string.
If you do not escape the backslash, then the \b
in the string literal is interpreted as a backspace character.
Upvotes: 5