Why does this pattern fail to compile :
Pattern.compile("(?x)[ ]\\b");
Error
ERROR java.util.regex.PatternSyntaxException:
Illegal/unsupported escape sequence near index 8
(?x)[ ]\b
^
at java_util_regex_Pattern$compile.call (Unknown Source)
While the following equivalent ones work?
Pattern.compile("(?x)\\ \\b");
Pattern.compile("[ ]\\b");
Pattern.compile(" \\b");
Is this a bug in the Java regex compiler, or am I missing something? I like to use [ ]
in verbose regex instead of backslash-backslash-space because it saves some visual noise. But apparently they are not the same!
PS: this issue is not about backslashes. It's about escaping spaces in a verbose regex using a character class containing a single space [ ]
instead of using a backslash.
Somehow the combination of verbose regex (?x)
and a character class containing a single space [ ]
throws the compiler off and makes it not recognize the word boundary escape \b
Tested with Java up to 1.8.0_151
I like to use
[ ]
in verbose regex instead of backslash-backslash-space because it saves some visual noise. But apparently they are not the same!
"[ ]"
is the same as "\\ "
or even " "
.
The problem is the (?x)
at the beginning enabling comments mode. As the documentation states
Permits whitespace and comments in pattern.
In this mode, whitespace is ignored, and embedded comments starting with#
are ignored until the end of a line.
Comments mode can also be enabled via the embedded flag expression(?x)
.
In comments mode the regex "(?x)[ ]\\b"
is the same as "[]\\b"
and won't compile because the empty character class []
is not parsed as empty, but parsed like "[\\]"
(unclosed character class containing a literal ]
).
Use " \\b"
instead. Alternatively, preserve the space in comments mode by escaping it with a backslash: "(?x)[\\ ]\\b"
or "(?x)\\ \\b"
.
This is a bug in Java's peekPastWhitespace()
method in the Pattern
class. Tracing this entire issue down... I decided to take a look at OpenJDK 8-b132's Pattern
implementation. Let's start hammering this down from the top:
compile()
calls expr()
on line 1696expr()
calls sequence()
on line 1996sequence()
calls clazz()
on line 2063 since the case of [
was metclazz()
calls peek()
on line 2509peek()
calls peekPastWhitespace()
on line 1830 since if(has(COMMENTS))
evaluates to true
(due to having added the x
flag (?x)
at the beginning of the pattern)peekPastWhitespace()
(posted below) skips all spaces in the pattern.peekPastWhitespace()
private int peekPastWhitespace(int ch) {
while (ASCII.isSpace(ch) || ch == '#') {
while (ASCII.isSpace(ch))
ch = temp[++cursor]
if (ch == '#') {
ch = peekPastLine();
}
}
return ch;
}
The same bug exists in the parsePastWhitespace()
method.
Your regex is being interpreted as []\\b
, which is the cause of your error because \b
is not supported in a character class in Java. Moreover, once you fix the \b
issue, your character class also doesn't have a closing ]
.
What you can do to fix this problem:
\\
As the OP mentioned, simply use double backslash and space[\\ ]
Escape the space within the character class so that it gets interpreted literally[ ](?x)\\b
Place the inline modifier after the character classIt looks like because of free-spacing (verbose) mode (?x)
space in [ ]
is ignored, so regex engine sees your regex as []\\b
.
If we remove \\b
it would be seen like []
and we would get error about Unclosed character class
- character class can't be empty so ]
placed directly after [
is treated as first character which belongs to that class instead of meta symbol which is closing character class.
So since [
is unclosed, regex engine sees \b
as being placed inside that character class. But \b
can't be placed there (it doesn't represent character but "place") so we are seeing error about "unsupported escape sequence" (inside character class, but that part was skipped).
In other words you can't use [ ]
to escape space in verbose mode (at least in Java). You would need to either use "\\ "
or "[\\ ]"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With