Ahmed Salah Ahmed Salah - 1 year ago 85
Perl Question

Match balanced brackets after certain string

I am having trouble matching balanced brackets after a certain string"if". I know that I can match balanced brackets using

((?>[^()]|(?R))?\)


and that works just fine. However, when I add my string "if" before the this regular expression to be

if[ \t]*((?>[^()]|(?R))?\)


It only matches single balanced brackets after "if".

online demo: https://regex101.com/r/6lg0qi/3

Answer Source

(?R) means recursing whole pattern while you need to recurs chunk of it:

if[ \t]*(\((?>[^()]+|(?1))*\))

Live demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download