Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...