Cedric Mamo
Cedric Mamo

Reputation: 1754

match opening parenthesis to the corresponding closing parenthesis

I am using java for my program
suppose i have a string such as this

xx(yyzz(iijj))qq((kkll)(gghh))

Is there any way in which i could match xx(yyzz(iijj)) and qq((kkll)(gghh)) separately using a regex?

Upvotes: 2

Views: 3475

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170308

You can match nested parenthesis using regex up to a fixed level. But more than 2 level will become rather messy (2 already is, to be frank). This will match your examples:

\(([^()]*+|\([^()]*+\))*\)

A quick explanation:

\(              # match a '(' 
(               # open group 1
  [^()]*+       #   match any chars other than '(' and ')'
  |             #   OR
  \([^()]*+\)   #   match '(...)'
)*              # close group 1 and repeat it zero or more times
\)              # match a '(' 

See the demo on ideone.com

There are regex flavors that can match an arbitrary number of nesting (Perl, .NET, PHP), but Java is not one of them.

But looking at the comment you posted under your question, I'd not handle this with regex, but a proper parser (be it a handcrafted one, or a generated).

Upvotes: 4

The Real Baumann
The Real Baumann

Reputation: 1961

The simple answer is no, there's not a way to do it using just a regular expression. Just iterate through the string and push the open parentheses onto a stack. Pop when you hit closed parentheses. If you try to pop or you finish and the stack isn't empty then it's invalid.

You could also do this recursively by removing the first index of '(' and lastIndex of ')' verifying that the index of '(' is less than the index of ')'

Upvotes: 7

Related Questions