0

Following strings - match:

"MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "MNO"
"MNO(A=(B=C) D=(E=F))" - "MNO"
"MNO" - "MNO"
"RAX.MNO(A=(B=C) D=(E=F)) PQR(X=(G=H) I=(J=(K=L)))" - "RAX.MNO"
"RAX.MNO(A=(B=C) D=(E=F))" - "RAX.MNO"
"RAX.MNO" - "RAX.MNO"

Inside every brace, there can be unlimited groups of them, but they have to be closed properly.

Any ideas? Don't know how to test properly for closure.

I have to use a Perl-Regular-Expression.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RaphaelH
  • 2,144
  • 2
  • 30
  • 43

1 Answers1

4

In Perl or PHP, for example, you could use a regex like

/\((?:[^()]++|(?R))*\)/

to match balanced parentheses and their contents.

See it on regex101.

To remove all those matches from a string $subject in Perl, you could use

$subject =~ s/\((?:[^()]++|(?R))*\)//g;

Explanation:

\(       # Match a (
(?:      # Start of non-capturing group:
 [^()]++ # Either match one or more characters except (), don't backtrack 
|        # or
 (?R)    # Match the entire regex again, recursively
)*       # Any number of times
\)       # Match a )
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • @RaphaelH: This regex works in Perl or PCRE (Perl-compatible regular expressions). – Tim Pietzcker Sep 06 '13 at 08:59
  • +1 Nice one. This is also mentioned in [perldoc perlre](http://perldoc.perl.org/perlre.html#%28?_PARNO_%29-%28?-_PARNO_%29-%28?+_PARNO_%29-%28?R%29-%28?0%29). It would be nice with a more detailed explanation. – TLP Sep 06 '13 at 09:11