Ahmed Salah
Ahmed Salah

Reputation: 161

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

Upvotes: 0

Views: 97

Answers (1)

revo
revo

Reputation: 48711

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

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

Live demo

Upvotes: 3

Related Questions