Regular expression pattern explanation

I’m fairly new to regular expressions, I recently used a regular expression in a perl script, and I can’t figure it out:

$groups= qr/\(([^()]+|(??{$groups}))*\)/;

Any help will be greatly appreciated!

Well, if you expand it:

$groups = qr/
\( # match an open paren (
(# followed by
[^()]+ # one or more non-paren character
| # OR
(??{$groups}) # the regex itself
)* # repeated zero or more times
\) # followed by a close paren )
/x;

You will get an elegant recursive method to find balanced brackets:)

I am fairly new to regular expressions, I recently I used a regular expression in the perl script and I can’t figure it out:

$groups= qr/\(([^()]+|(? ?{$groups}))*\)/;

Any help would be greatly appreciated!

Well, if you extend it:

$groups= qr/
\( # match an open paren (
(# followed by
[^()]+ # one or more non-paren character
| # OR
(?? {$groups}) # the regex itself
)* # repeated zero or more times
\) # followed by a close paren )
/x;

You will Get an elegant recursive method to find balanced brackets:)

Leave a Comment

Your email address will not be published.