Regular Expression - Grouping Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IKAS

    #1

    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 "WhereClaus e" 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.
  • Chris R. Timmons

    #2
    Re: Regular Expression - Grouping Question

    ikassaki@hotmai l.com (IKAS) wrote in
    news:cec61ad7.0 307210326.6bbde 12f@posting.goo gle.com:
    [color=blue]
    > 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 "WhereClaus e" 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 ?[/color]

    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+(?<Se lectClause>.*)\ s+from\s+(?<Fro mClause>.*?)(\s +where\s+
    (?<WhereClause> .*))*$


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.

    Comment

    • ikas

      #3
      Re: Regular Expression - Grouping Question

      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...s tupid
      me...(;-))

      Thanks,
      IKAS




      Comment

      Working...