Fantoma Din Umbra
Fantoma Din Umbra

Reputation: 99

Regular expression take correct number of brackets

\w+\(([^\)]*)?\)+

This regex will match

abc(1,3,abs(4) 
foo(1,3,abs(4)))

I want to match only

abc(1,3,abs(4))

Is it possible?

Upvotes: 1

Views: 48

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use the following regex:

^[a-z]+\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)$

It will match any string of characters from a to z in the beginning, and then a matching number of parentheses and everything inside them.

A demo on regexstorm

Tested in Expresso:

enter image description here

Upvotes: 2

Related Questions