2

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?

4

2 回答 2

5

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.

于 2012-10-11T16:23:15.687 回答
2

改用这个: -

Pattern insPtrn = Pattern.compile("\\bINSERT\\b")

你需要\b用一个额外的反斜杠转义..

于 2012-10-11T16:23:51.427 回答