find matching parentheses in a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to extract the text within the matching parentheses.

Eg Input: (S1 > 1 and S2 in ('a','b','c') and (S5 > '05/27/04' and S5 <
'05/27/05') and (S4 = 'hello' or S4 = 'world') and (S_price - S_price2))
or (S1 > 1 and S3 = '12/12/03' and (S5 > 1) and (S_price - S_price2))
or (S1 > 1 and S2 in ([AGR1]) and S3 = [AGR2] and (S_price - S_price2))

For the above input, my result should like below (for the 3 outer arguments)
1) S1 > 1 and S2 in ('a','b','c') and (S5 > '05/27/04' and S5 < '05/27/05')
and (S4 = 'hello' or S4 = 'world') and (S_price - S_price2)
2) S1 > 1 and S3 = '12/12/03' and (S5 > 1) and (S_price - S_price2)
3) S1 > 1 and S2 in ([AGR1]) and S3 = [AGR2] and (S_price - S_price2)

Is this possible using RegEx? If so, what is the pattern I'll need to use?

Thanks.
 
UK said:
I want to extract the text within the matching parentheses.

Eg Input: (S1 > 1 and S2 in ('a','b','c') and (S5 > '05/27/04' and S5 <
'05/27/05') and (S4 = 'hello' or S4 = 'world') and (S_price - S_price2))
or (S1 > 1 and S3 = '12/12/03' and (S5 > 1) and (S_price - S_price2))
or (S1 > 1 and S2 in ([AGR1]) and S3 = [AGR2] and (S_price - S_price2))

For the above input, my result should like below (for the 3 outer
arguments)
1) S1 > 1 and S2 in ('a','b','c') and (S5 > '05/27/04' and S5 < '05/27/05')
and (S4 = 'hello' or S4 = 'world') and (S_price - S_price2)
2) S1 > 1 and S3 = '12/12/03' and (S5 > 1) and (S_price - S_price2)
3) S1 > 1 and S2 in ([AGR1]) and S3 = [AGR2] and (S_price - S_price2)

Is this possible using RegEx? If so, what is the pattern I'll need to use?

Try using this pattern:

\([^()]*(((?<Open>\()[^()]*)+((?<Close-Open>\))[^()]*)+)*(?(Open)(?!))\)

This has been constructed from the template given by Ryan Byington at
http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx

Just in case you're curious how it works :-)



Oliver Sturm
 
Back
Top