You should be able to use a regular expression with a balancing group
definition to count the nested paretheses, but I can't get it to work. So
this is an answer and a question.
The following VB.NET code should return a match of "(yes (here) okay)",
because it is the largest expression that contains equally paired
parentheses. It does not work using framework 1.1. Someone please rescue me
from my folly!
Dim m As Match = Regex.Match("before (nope (yes (here) okay) after", _
"\( " & _
" (?> " & _
" [ˆ()]+ " & _
" | " & _
" \( (?<DEPTH>) " & _
" | " & _
" \) (?<-DEPTH>) " & _
" )+ " & _
" (?(DEPTH)(?!)) " & _
"\)", _
RegexOptions.IgnorePatternWhitespace)
There is a VERY brief explanation of balancing group definitions at the
following MSDN link.
http://msdn.microsoft.com/library/de...asp?frame=true
I got this code and the concepts behind it from page 430 of the following PDF.
http://www.oreilly.com/catalog/regex2/chapter/ch09.pdf
"Juan Dent" wrote:
> Hi,
>
> I have been struggling with the most clear and efficient solution to the
> following:
>
> I have a SELECT string that may contain a call to the CONVERT() function call.
> This call may have 2 or 3 arguments and some of these arguments could also
> have their own function calls, like so;
>
> CONVERT( char(32), LOWER(dep.RecordDate))
>
> I need to extract the entire parameters, which would be so simple if there
> were no embedded parenthesis in the parameters.
>
> Thus I would like to have something like:
>
> Match m = RegEx.XXXX(...);
> m.Groups[1].Value => char(32) and
> m.Groups[2].Value => LOWER(dep.RecordDate)
>
> Is this possible using a single regular expression? (I could of course parse
> first to determine the number of parameters and their positions and from
> there obtain the information I need, but I have a feeling, from what I have
> seen, that RegEx is much more powerful, and I would like to take the
> oportunity to better learn to use this power.
>
> Any comments?
> --
> Thanks in advance,
>
> Juan Dent, M.Sc.