Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

Java Regex Pattern compilation error

I am trying to use the following regex pattern B@(@?w+(?:::w+)?)([ ]*)(( ( (?>[^()]+) | (?3) )* ))? using java.util.regex.Pattern but I keep getting the error Unknown inline modifier near index 49 B@(@?w+(?:::w+)?)([ ]*)(( ( (?>[^()]+) | (?3) )* ))? ^ I have tried to escape the regex pattern using the character at the index it is complaining about but it still fails. Hoping someone here can help me get this working.

This is the test string I am trying to use it against:

Value @if(blah == 1) 'assigned' @else 'reassigned' @endif from boom to blah

If I put the pattern into the website regex 101 it works fine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(?3) isn't valid in Java. It is parsed as an "inline modifier" as mentioned in the error message, which is a way to activate a flag for the remainder of the regex (or until an opposite (?-X) is encountered), for example (?i) to enable case-insensitive search. There's no flag named 3, hence the error.

It is however valid in some PCRE implementations (most notably in Perl which is the reference implementation for PCRE) and makes it possible to refer to a capturing group, enabling at the same time the possibility to define recursive patterns. This is how it is used in this regex.

Rewriting the regex to be compatible with Java would require some non-trivial work, and it would be interesting to ponder whether a regex implementation is still preferable to some other code without this feature.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...