-1

This regex "((?:[^()]|.((?R)))+)" is working on Regex101 site but it raises a sintaxException with JAVA matcher.

Error: Caused by: java.util.regex.PatternSyntaxException: Unknown inline modifier near index 14 \((?:[^()]|((?R)))+\)

The "R" seens not accepted by java regex engine. Unfortunately this seems to be the only way to get the match I need:

Assuming this is the source string I have to parse:

my params ( string(80), string(30), string(10) ) as ...

I need to get the content between the first "(" and the closing ")" (the last before as). This is just an example, and I have no other "hooks" except the "mathematical sequence" of opened and closed parenthesis. Any suggestion on how to solve this problem (in a way or another?) The regex is working on many regex testing sites, but not working using it in Java.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Previously closed as duplicate of [Is it possible to match nested brackets with a regex without using recursion or balancing groups?](https://stackoverflow.com/questions/47162098/is-it-possible-to-match-nested-brackets-with-a-regex-without-using-recursion-or) – Ole V.V. Mar 03 '23 at 17:34
  • 1
    Unfortunately, the Java regex engine doesn't have the recursion feature nor the balancing group feature. All you can do with regex in Java is to use the fact that a limited variable length in a lookbehind is allowed, but you can't do all with that. The most robust way is to use a parser (if there's already a parser for what you are trying to parse), or to tokenize your string and to count yourself the number of opening and closing brackets. – Casimir et Hippolyte Mar 03 '23 at 21:25
  • Try this `(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2)` – sln Mar 03 '23 at 22:35
  • It comes from this https://stackoverflow.com/questions/47162098/is-it-possible-to-match-nested-brackets-with-a-regex-without-using-recursion-or – sln Mar 03 '23 at 22:48

1 Answers1

0

if the regex is working on many regex testing sites, you should use \((?:[^()]|((\?R)))+\) instead of \((?:[^()]|((?R)))+\) as it shows ? The preceding token is not quantifiable as the ? is a ternary operator in Java.
But the regex will not run in Java as Java does not support Regex Subroutine yet, so please use (?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2) provided by sln.

Sarkar
  • 56
  • 6