Error compiling a verbose Java regex with character class and word boundary

邮差的信 提交于 2021-02-06 14:26:45

问题


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


回答1:


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:

  1. compile() calls expr() on line 1696
  2. expr() calls sequence() on line 1996
  3. sequence() calls clazz() on line 2063 since the case of [ was met
  4. clazz() calls peek() on line 2509
  5. peek() 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)
  6. 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:

  1. \\ As the OP mentioned, simply use double backslash and space
  2. [\\ ] Escape the space within the character class so that it gets interpreted literally
  3. [ ](?x)\\b Place the inline modifier after the character class



回答2:


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




回答3:


It 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 "[\\ ]".




回答4:


A workaround

Beside escaping whitespaces separately that are literally the same as [ ], you could have x mode on for entire regex but disable it while working on patterns that need whitespaces, inline:

(?x)match-this-(?-x: with spaces )\\b
    ^^^^^^^^^^^     ^^^^^^^^^^^^^ ^^^
    `x` is on            off       on

or an alternative would be using qouting meta-characters \Q...\E:

(?x)match-this-\Q with s p a c e s \E\\b
    ^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^  ^^^
    `x` is on            off          on

Why an Exception?

In extended or comment mode (x) whitespaces are ignored but dealing with spaces within character classes in various flavors is handled differently.

For example in PCRE all whitespace characters are ignored except those in a character class. That means [ ] is a valid regex but Java doesn't have an exception:

In this mode, whitespace is ignored...

Period. So this [ ] is equal to this [] which is not valid and throws a PatternSyntaxException exception.

Almost all regex flavors except JavaScript needs a character class to have at least one data unit. They treat an empty character class as an unclosed set which needs a closing bracket. Saying that, []] is valid in most flavors.

Free spacing mode in defferent flavors on [ ]:

  • PCRE valid
  • .NET valid
  • Perl valid
  • Ruby valid
  • TCL valid
  • Java 7 Invalid
  • Java 8 Invalid



回答5:


Lets analyse what happen exactly.

Take a look at the source code of java.util.regex.Pattern

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

Your regex guide you to this line

private void accept(int ch, String s) {
    int testChar = temp[cursor++];
    if (has(COMMENTS))
        testChar = parsePastWhitespace(testChar);
    if (ch != testChar) {
        throw error(s);
    }
}

If you notice your code call parsePastWhitespace(testChar);

private int parsePastWhitespace(int ch) {
    while (ASCII.isSpace(ch) || ch == '#') {
        while (ASCII.isSpace(ch))//<----------------Here is the key of your error
            ch = temp[cursor++];
        if (ch == '#')
            ch = parsePastLine();
    }
    return ch;
}

In your case you have a white space in your regular expression (?x)[ ]\\b this will return something (I can't analyse it correctly) :

    if (ch != testChar) {
        throw error(s);
    }

which is not equal to ch and here a exception is throws

throw error(s);


来源:https://stackoverflow.com/questions/49264034/error-compiling-a-verbose-java-regex-with-character-class-and-word-boundary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!