PC Review


Reply
Thread Tools Rate Thread

Recursive regular expressions

 
 
=?Utf-8?B?SnVhbiBEZW50?=
Guest
Posts: n/a
 
      22nd May 2005
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.
 
Reply With Quote
 
 
 
 
Lloyd Dupont
Guest
Posts: n/a
 
      22nd May 2005
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

"Juan Dent" <(E-Mail Removed)> wrote in message
news:E575A561-4DD7-440A-A471-(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Nicolas Guinet
Guest
Posts: n/a
 
      23rd May 2005
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


 
Reply With Quote
 
=?Utf-8?B?SnVhbiBEZW50?=
Guest
Posts: n/a
 
      23rd May 2005
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?
--
Thanks in advance,

Juan Dent, M.Sc.


"Nicolas Guinet" wrote:

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

 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      24th May 2005
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/archi...30/102146.aspx

Building a basic parser
http://weblogs.asp.net/dneimke/archi.../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.

 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      24th May 2005
it's what I said, you need to do a parser!
go there:
www.ANTLR.org !!

""Peter Huang" [MSFT]" <v-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/archi...30/102146.aspx
>
> Building a basic parser
> http://weblogs.asp.net/dneimke/archi.../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.
>



 
Reply With Quote
 
=?Utf-8?B?V3lhdHQgSGFhc2U=?=
Guest
Posts: n/a
 
      22nd Oct 2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions RN1 Microsoft ASP .NET 7 31st Mar 2008 08:48 PM
Regular Expressions? Arpan Microsoft ASP .NET 5 28th Jul 2006 02:20 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas Microsoft Dot NET Framework 0 16th Aug 2003 01:25 AM
Add custom regular expressions to the validation list of available expressions Jay Douglas Microsoft ASP .NET 0 16th Aug 2003 12:19 AM
Re: Help with regular expressions. David Waz... Microsoft ASP .NET 0 4th Jul 2003 10:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:19 PM.