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"
.