0

I am trying to use the following regex pattern \B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))? using java.util.regex.Pattern but I keep getting the error Unknown inline modifier near index 49 \B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?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.

1
  • Following NahuelFouilleul and SebastianProske's comments it seems likely that the pattern you're trying to use really means to use (?3), by which it would reference the third capturing group. That's a valid syntax in some PCRE implementations, but isn't implemented in Java. Replacing it with 3 will make the regex compile, but it won't match what you expect. Commented Jan 10, 2018 at 15:01

1 Answer 1

3

(?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.

Sign up to request clarification or add additional context in comments.

7 Comments

That was it! Thank you so much
(?3) can be valid for engine which support recursive regex which seems to be the case of question because third capturing group matches balanced parentheses
(?3) in PCRE is a recursion to the third subpattern, this looks very much like a pattern to match nested parenthesis. Java regex patterns do not support recursion. Regex101 uses PCRE as default, that's the reason it works over there.
@NahuelFouilleul & SebastianProske Java's regex engine is PCRE-based but doesn't implement all features and can differ from Perl's reference implementation on some points. I don't think it implements recursive patterns, have you got any evidence it does? (I will test it, but if you have documentation that would be faster and better :) )
no it's not supported in java, but the pattern in the question suggests a recurisve regex
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.