Re: ...newbie reg expression for this replacement...

Discussion in 'ASP .Net' started by Daniel Bass, Oct 10, 2003.

  1. Daniel Bass

    Daniel Bass Guest

    I want to convert the following example

    string query = "Type in ('1','hello','3')";

    into

    newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )



    a more general syntax would be

    {ID} in ( '{value1}', '{value2}', ... '{valueN}' )

    to be converted into

    ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    '{valueN}' )

    where N is any positive integer


    I figure the best way to do this is to use regular expressions, but I've
    just starting looking at them and haven't a clue where to start.

    Thanks for your time.
    Dan.
     
    Daniel Bass, Oct 10, 2003
    #1
    1. Advertising

  2. Daniel Bass

    steve Guest

    they accomplish the same thing...is there a performance difference?

    i can give you the answer but i don't see the value in it.

    steve


    "Daniel Bass" <> wrote in message
    news:...
    > I want to convert the following example
    >
    > string query = "Type in ('1','hello','3')";
    >
    > into
    >
    > newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )
    >
    >
    >
    > a more general syntax would be
    >
    > {ID} in ( '{value1}', '{value2}', ... '{valueN}' )
    >
    > to be converted into
    >
    > ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    > '{valueN}' )
    >
    > where N is any positive integer
    >
    >
    > I figure the best way to do this is to use regular expressions, but I've
    > just starting looking at them and haven't a clue where to start.
    >
    > Thanks for your time.
    > Dan.
    >
    >
    >
     
    steve, Oct 10, 2003
    #2
    1. Advertising

  3. I believe you're better off with the one you started with.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "Daniel Bass" <> wrote in message
    news:...
    > I want to convert the following example
    >
    > string query = "Type in ('1','hello','3')";
    >
    > into
    >
    > newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )
    >
    >
    >
    > a more general syntax would be
    >
    > {ID} in ( '{value1}', '{value2}', ... '{valueN}' )
    >
    > to be converted into
    >
    > ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    > '{valueN}' )
    >
    > where N is any positive integer
    >
    >
    > I figure the best way to do this is to use regular expressions, but I've
    > just starting looking at them and haven't a clue where to start.
    >
    > Thanks for your time.
    > Dan.
    >
    >
    >
     
    Kevin Spencer, Oct 10, 2003
    #3
  4. Daniel Bass

    Brian Davis Guest

    Below is an expression that may be useful:

    (?<id>\w+) in \(((?<expr>'\w+')(,|\)$))+

    You could then use a MatchEvaluator function with Regex.Replace to perform
    the replace. The values can be found in the Captures Collection of the expr
    Group.

    Here is some sample VB.NET code:

    Sub Process()
    Dim sSource As String = "Type in ('1','hello','3')"
    Dim sResult As String

    Dim reReplaceIn As New Regex("(?<id>\w+) in
    \(((?<expr>'\w+')(,|\)$))+", RegexOptions.Compiled)

    sResult = reReplaceIn.Replace(sSource, AddressOf PerformReplace)

    'Do something with sResult
    End Sub

    Function PerformReplace(ByVal m As Match) As String
    Dim sResult As String
    Dim c As Capture
    Dim bFirstCapture As Boolean = True

    sResult = "("

    For Each c In m.Groups("expr").Captures
    If Not bFirstCapture Then
    sResult &= " OR "
    End If
    sResult &= m.Groups("id").Value & " = " & c.Value
    bFirstCapture = False
    Next

    sResult &= ")"

    Return sResult
    End Function



    Brian Davis
    www.knowdotnet.com


    "Daniel Bass" <> wrote in message
    news:...
    > I want to convert the following example
    >
    > string query = "Type in ('1','hello','3')";
    >
    > into
    >
    > newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )
    >
    >
    >
    > a more general syntax would be
    >
    > {ID} in ( '{value1}', '{value2}', ... '{valueN}' )
    >
    > to be converted into
    >
    > ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    > '{valueN}' )
    >
    > where N is any positive integer
    >
    >
    > I figure the best way to do this is to use regular expressions, but I've
    > just starting looking at them and haven't a clue where to start.
    >
    > Thanks for your time.
    > Dan.
    >
    >
    >
     
    Brian Davis, Oct 10, 2003
    #4
  5. Daniel Bass

    steve Guest

    i know it's just an example but this is more for the op than you...if you're
    going to use the compile option of the regexp and it is a static expression,
    declare and instanciate your regexp variable in your declares section and
    not w/n a function...it's a performance issue.

    hth,

    steve

    good exp btw.


    "Brian Davis" <@> wrote in message
    news:...
    > Below is an expression that may be useful:
    >
    > (?<id>\w+) in \(((?<expr>'\w+')(,|\)$))+
    >
    > You could then use a MatchEvaluator function with Regex.Replace to perform
    > the replace. The values can be found in the Captures Collection of the

    expr
    > Group.
    >
    > Here is some sample VB.NET code:
    >
    > Sub Process()
    > Dim sSource As String = "Type in ('1','hello','3')"
    > Dim sResult As String
    >
    > Dim reReplaceIn As New Regex("(?<id>\w+) in
    > \(((?<expr>'\w+')(,|\)$))+", RegexOptions.Compiled)
    >
    > sResult = reReplaceIn.Replace(sSource, AddressOf PerformReplace)
    >
    > 'Do something with sResult
    > End Sub
    >
    > Function PerformReplace(ByVal m As Match) As String
    > Dim sResult As String
    > Dim c As Capture
    > Dim bFirstCapture As Boolean = True
    >
    > sResult = "("
    >
    > For Each c In m.Groups("expr").Captures
    > If Not bFirstCapture Then
    > sResult &= " OR "
    > End If
    > sResult &= m.Groups("id").Value & " = " & c.Value
    > bFirstCapture = False
    > Next
    >
    > sResult &= ")"
    >
    > Return sResult
    > End Function
    >
    >
    >
    > Brian Davis
    > www.knowdotnet.com
    >
    >
    > "Daniel Bass" <> wrote in message
    > news:...
    > > I want to convert the following example
    > >
    > > string query = "Type in ('1','hello','3')";
    > >
    > > into
    > >
    > > newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )
    > >
    > >
    > >
    > > a more general syntax would be
    > >
    > > {ID} in ( '{value1}', '{value2}', ... '{valueN}' )
    > >
    > > to be converted into
    > >
    > > ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    > > '{valueN}' )
    > >
    > > where N is any positive integer
    > >
    > >
    > > I figure the best way to do this is to use regular expressions, but I've
    > > just starting looking at them and haven't a clue where to start.
    > >
    > > Thanks for your time.
    > > Dan.
    > >
    > >
    > >

    >
    >
     
    steve, Oct 10, 2003
    #5
  6. Daniel Bass

    Brian Davis Guest

    Yeah, you're right. I started the example as an entire class and had it at
    module level, but then I got lazy and decided to just make it a couple of
    methods. It is a good point, as you often see the Compiled option used
    needlessly just because it sounds cool.

    Brian Davis
    www.knowdotnet.com



    "steve" <> wrote in message
    news:...
    > i know it's just an example but this is more for the op than you...if

    you're
    > going to use the compile option of the regexp and it is a static

    expression,
    > declare and instanciate your regexp variable in your declares section and
    > not w/n a function...it's a performance issue.
    >
    > hth,
    >
    > steve
    >
    > good exp btw.
    >
    >
    > "Brian Davis" <@> wrote in message
    > news:...
    > > Below is an expression that may be useful:
    > >
    > > (?<id>\w+) in \(((?<expr>'\w+')(,|\)$))+
    > >
    > > You could then use a MatchEvaluator function with Regex.Replace to

    perform
    > > the replace. The values can be found in the Captures Collection of the

    > expr
    > > Group.
    > >
    > > Here is some sample VB.NET code:
    > >
    > > Sub Process()
    > > Dim sSource As String = "Type in ('1','hello','3')"
    > > Dim sResult As String
    > >
    > > Dim reReplaceIn As New Regex("(?<id>\w+) in
    > > \(((?<expr>'\w+')(,|\)$))+", RegexOptions.Compiled)
    > >
    > > sResult = reReplaceIn.Replace(sSource, AddressOf PerformReplace)
    > >
    > > 'Do something with sResult
    > > End Sub
    > >
    > > Function PerformReplace(ByVal m As Match) As String
    > > Dim sResult As String
    > > Dim c As Capture
    > > Dim bFirstCapture As Boolean = True
    > >
    > > sResult = "("
    > >
    > > For Each c In m.Groups("expr").Captures
    > > If Not bFirstCapture Then
    > > sResult &= " OR "
    > > End If
    > > sResult &= m.Groups("id").Value & " = " & c.Value
    > > bFirstCapture = False
    > > Next
    > >
    > > sResult &= ")"
    > >
    > > Return sResult
    > > End Function
    > >
    > >
    > >
    > > Brian Davis
    > > www.knowdotnet.com
    > >
    > >
    > > "Daniel Bass" <> wrote in message
    > > news:...
    > > > I want to convert the following example
    > > >
    > > > string query = "Type in ('1','hello','3')";
    > > >
    > > > into
    > > >
    > > > newquery = ( Type = '1' OR Type = 'hello' OR Type = '3' )
    > > >
    > > >
    > > >
    > > > a more general syntax would be
    > > >
    > > > {ID} in ( '{value1}', '{value2}', ... '{valueN}' )
    > > >
    > > > to be converted into
    > > >
    > > > ( {ID} = '{value1}' OR {ID} = '{value2}' OR ... OR {ID} =
    > > > '{valueN}' )
    > > >
    > > > where N is any positive integer
    > > >
    > > >
    > > > I figure the best way to do this is to use regular expressions, but

    I've
    > > > just starting looking at them and haven't a clue where to start.
    > > >
    > > > Thanks for your time.
    > > > Dan.
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Brian Davis, Oct 10, 2003
    #6
  7. Daniel Bass

    Daniel Bass Guest

    Thanks for your help!

    It's just what I was looking for. =o)
    Dan.
     
    Daniel Bass, Oct 13, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lyn
    Replies:
    5
    Views:
    539
    Terry Michaels
    May 6, 2004
  2. Brian Henry

    reg expression for date validation

    Brian Henry, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,727
    Kevin Spencer
    Dec 5, 2003
  3. Pat

    reg expression

    Pat, Sep 15, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    414
    naijacoder naijacoder
    Sep 15, 2005
  4. Red Ogden

    xsd reg expression for wildcard

    Red Ogden, Jun 25, 2003, in forum: XML
    Replies:
    1
    Views:
    971
    Red Ogden
    Jun 26, 2003
  5. Colin Mackenzie

    Re: xsd reg expression for not

    Colin Mackenzie, Jun 30, 2003, in forum: XML
    Replies:
    3
    Views:
    1,348
    Red Ogden
    Jul 2, 2003
Loading...

Share This Page