473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular Expression - Grouping Question

I am having some difficulty in using Regular expression in .NET with
C#.

How do I write a regular expression so that it will match both the
following strings and also get me the relevant named groups ?

1. select xcol, ycol from sampletable where zcol='val'

2. select xcol, ycol from sampletable
If I try the following regular expression

^select (?<SelectClause>.*) from (?<FromClause>.*) (where
(?<WhereClause>.*))*$

and execute it with first string the "WhereClause" group is not
matched.
Even worse, the where clause part shows up in the "FromClause" group.

Maybe there is a better way to achieve what I am trying to do ?

Any help is appreciated.

Thanx in advance.
Nov 15 '05 #1
2 4799
ik******@hotmail.com (IKAS) wrote in
news:ce**************************@posting.google.c om:
I am having some difficulty in using Regular expression in .NET
with C#.

How do I write a regular expression so that it will match both
the following strings and also get me the relevant named groups?

1. select xcol, ycol from sampletable where zcol='val'

2. select xcol, ycol from sampletable

If I try the following regular expression

^select (?<SelectClause>.*) from (?<FromClause>.*) (where
(?<WhereClause>.*))*$

and execute it with first string the "WhereClause" group is not
matched.
Even worse, the where clause part shows up in the "FromClause"
group.

Maybe there is a better way to achieve what I am trying to do ?


The <FromClause> uses a greedy * quantifer. In this mode, * will
match as much text as it possibly can. Since the <WhereClause> is
optional, the <FromClause> * will "eat up" all of the text to the end
of the string.

To fix this, just put a ? after the * in <FromClause>:

^select (?<SelectClause>.*) from (?<FromClause>.*?)( where
(?<WhereClause>.*))*$

That turns the * quantifier into a non-greedy quantifier, and it will
only match the minimum amount of text necessary to fulfill its part
of the pattern.

Also note the change I made by putting the space before the "where"
keyword, because the space is really part of the optional
<WhereClause>.

Another change you might want to make is to use \s+ instead of hard-
coding the spaces to make the regex more flexible:

^select\s+(?<SelectClause>.*)\s+from\s+(?<FromClau se>.*?)(\s+where\s+
(?<WhereClause>.*))*$
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
Hi Chris !

Thanks a lot for your help.

I really appreciate for having taken the time to answer it.

I was actually trying some lazy constructs on the WhereClause...stupid
me...(;-))

Thanks,
IKAS


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
3
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing...
3
by: Gopinath | last post by:
Hi JavaScript Gurus, I've a question on Regular Expressions using RegExp object. I just want to know whether it is possible to do the search (see below) using RegExp. Any pointers would be of...
12
by: Laser Lu | last post by:
Hello, everybody, do you know how to use this Grouping Construct? (?> ) I've found its reference on MSDN, but still can not understand it totally. The following is its description: ...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.