Recursive regular expressions

G

Guest

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?
 
L

Lloyd Dupont

Ahum... I don't know much about RegularExpression.....
However when I want to do advance parsing, I use a real parser generator
tool:
www.ANTLR.org
 
N

Nicolas Guinet

Hi,

private void button1_Click(object sender, System.EventArgs e)
{
// using System.Text.RegularExpressions; // Parsing

try
{
string rex = @"CONVERT\( ?(?<G1>.*),(?<G2>.*)"; //
,(?<G3>.*),(?<G4>.*) ...
string test = "CONVERT( char(32),
LOWER(dep.RecordDate))";

MatchCollection matches = Regex.Matches(test, rex,
RegexOptions.Singleline|RegexOptions.IgnoreCase);

foreach ( Match match in matches )
{
textBox1.Text = match.Groups["G1"].Value.Trim() +
"\r\n";
textBox1.Text += match.Groups["G2"].Value.Trim();
}

}
catch (Exception ee)
{
Text = ee.Message;
}
}


Nicolas Guinet
 
G

Guest

I probably did not explain myself clearly, and my example was far too simple.
What I am after is the ability to specify a recursive pattern that would
match very different strings (although they have the same recursive
definition of functions within functions or put another way, pair of
parentheses that might contain embedded pairs of parentheses, indefinitely):

CONVERT(char(f(g(21),10)), LOWER(X(recordDate,7)))

OR

CONVERT(char(32), recordDate)


where, of course, f, g and X are any functions - they just illustrate the
recursiveness I am after.

Tough?
 
P

Peter Huang [MSFT]

Hi

I think this is not a simple pattern match.
It is more of a Lexical analyze in complier area.
I think we need to understand the Lexical analyze to generate the proper
regex.

Here are some links for your reference.

Using BNF to create regex's
http://weblogs.asp.net/dneimke/archive/2004/03/30/102146.aspx

Building a basic parser
http://weblogs.asp.net/dneimke/archive/2004/03/24/94832.aspx

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

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/d...f/html/cpcongroupingconstructs.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top