Java regex look-behind group does not have obvious maximum length error

前端 未结 4 2100
情书的邮戳
情书的邮戳 2021-01-05 03:56

I know that java regex does not support varying length look-behinds, and that the following should cause an error

(?<=(not exceeding|no((\\\\w|\\\\s)*)mor         


        
4条回答
  •  旧时难觅i
    2021-01-05 04:14

    Java Lookbehind is Notoriously Buggy

    So you thought Java did not support infinite lookbehind?

    But the following pattern will compile!

    (?<=\d+)\w+
    

    ...though in a Match All it will yield unexpected results (see demo).

    On the other hand, you can with success use this other infinite lookbehind (which I found with great surprise on this question)

    (?<=\\G\\d+,\\d+,\\d+),
    

    to split this string: 0,123,45,6789,4,5,3,4,6000

    It will correctly output (see the online demo):

    0,123,45
    6789,4,5
    3,4,6000
    

    This time the results are what you expect.

    But if you tweak the regex the slightest bit to obtain pairs instead of triplets, with (?<=\\G\\d+,\\d+),, this time it will not split (see the demo).


    The bottom line

    Java lookbehind is notoriously buggy. Knowing this, I recommend you don't waste time trying to understand why it does something that is undocumented.

    The decisive words that drove me to this conclusion some time ago are those from Jan Goyvaerts, who is a co-author of The Regex Cookbook and an arch-regex-guru who has created a terrific regex engine and needs to stay on top of most regex flavors under the sun for his debugging tool RegexBuddy:

    Java has a number of bugs in its lookbehind implementation. Some (but not all) of those were fixed in Java 6.

提交回复
热议问题