regular expression question

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

    #1

    regular expression question

    The primary question is how do I perform a match when the regular
    expression contains string variables? For example, in the following
    code I want to match a line that starts with STX, then has any number
    of characters, then ends with STX.
    Example 2 I'm pretty sure works as I expect, but I'm not sure about
    Example 1, and I'm pretty sure about example 3.

    import re
    from curses.ascii import STX,ETX,FS
    STX = chr(STX)
    ETX = chr(ETX)
    FS = chr(FS)
    data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX
    match = STX + '(.*)' + ETX

    # Example 1
    # This appears to work, but I'm not sure if the '+' is being used in
    the regular expression, or if it's just joining STX, '(.*)', and ETX.

    if re.search(STX + '(.*)' + ETX,data):
    print "Matches"

    # Example 2
    # This also appears to work
    if re.search(match ,data):
    print "Matches"

    # Example 3
    # Doesn't work, as STX and ETX are evaluated as the literal strings
    'STX' and 'ETX'
    if re.search('STX( .*)ETX', data):
    print "Matches"

    Chris
  • Bruno Desthuilliers

    #2
    Re: regular expression question

    snacktime a écrit :[color=blue]
    > The primary question is how do I perform a match when the regular
    > expression contains string variables? For example, in the following
    > code I want to match a line that starts with STX, then has any number
    > of characters, then ends with STX.
    > Example 2 I'm pretty sure works as I expect, but I'm not sure about
    > Example 1, and I'm pretty sure about example 3.
    >
    > import re
    > from curses.ascii import STX,ETX,FS
    > STX = chr(STX)
    > ETX = chr(ETX)
    > FS = chr(FS)
    > data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX
    > match = STX + '(.*)' + ETX
    >
    > # Example 1
    > # This appears to work, but I'm not sure if the '+' is being used in
    > the regular expression, or if it's just joining STX, '(.*)', and ETX.
    >
    > if re.search(STX + '(.*)' + ETX,data):
    > print "Matches"
    >
    > # Example 2
    > # This also appears to work
    > if re.search(match ,data):
    > print "Matches"
    >
    > # Example 3
    > # Doesn't work, as STX and ETX are evaluated as the literal strings
    > 'STX' and 'ETX'
    > if re.search('STX( .*)ETX', data):
    > print "Matches"[/color]

    You may want something like:
    if re.search('%s(. *)%s' % (STX, ETX), data):
    ...

    BTW, given your requirements, I'd write this:
    if re.search('^%s( .*)%s$' % (STX, ETX), data):
    ...


    [color=blue]
    > Chris[/color]

    Comment

    • snacktime

      #3
      Re: regular expression question

      > You may want something like:[color=blue]
      > if re.search('%s(. *)%s' % (STX, ETX), data):
      >[/color]

      Ah I didn't even think about that...

      Chris

      Comment

      • Fredrik Lundh

        #4
        Re: regular expression question

        Bruno Desthuilliers wrote:
        [color=blue][color=green]
        >> match = STX + '(.*)' + ETX
        >>
        >> # Example 1
        >> # This appears to work, but I'm not sure if the '+' is being used in
        >> the regular expression, or if it's just joining STX, '(.*)', and ETX.
        >>
        >> if re.search(STX + '(.*)' + ETX,data):
        >> print "Matches"
        >>
        >> # Example 2
        >> # This also appears to work
        >> if re.search(match ,data):
        >> print "Matches"[/color][/color]
        [color=blue]
        > You may want something like:
        > if re.search('%s(. *)%s' % (STX, ETX), data):
        > ...[/color]

        that's of course the same thing as examples 1 and 2.

        a tip to the original poster: if you're not sure what an expression does,
        try printing the result. use "print repr(v)" if the value may contain odd
        characters. try adding this to your test script:

        print repr(match)
        print repr(STX + '(.*)' + ETX)
        print repr('%s(.*)%s' % (STX, ETX))

        </F>



        Comment

        Working...