regular expression question

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

    regular expression question

    Ok, this just seems convluted.

    import re
    line = 'match this'
    foo = re.search(r'.*( this)', line)
    foo.groups()[0]

    To me there seems there should be a way to return a group without having
    to get the tuple and then slice it out from there. However foo.group(0) will
    return the entire string (only one argument given) and groups doesn't seem to
    do anything significant with any entry I give it. Am I missing something
    obvious or is this the final solution?

    --
    Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
    PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
    -------------------------------+---------------------------------------------
  • Ben Finney

    #2
    Re: regular expression question

    On Wed, 21 Jan 2004 00:38:12 -0000, Steve Lamb wrote:[color=blue]
    > import re
    > line = 'match this'
    > foo = re.search(r'.*( this)', line)
    > foo.groups()[0][/color]
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> line = 'match this'
    >>> foo = re.search( r'.*(this)', line )
    >>> foo.groups()[/color][/color][/color]
    ('this',)[color=blue][color=green][color=darkred]
    >>> foo.group(0)[/color][/color][/color]
    'match this'[color=blue][color=green][color=darkred]
    >>> foo.group(1)[/color][/color][/color]
    'this'

    Read more on the methods available from a MatchObject:

    <http://www.python.org/doc/lib/match-objects.html>

    --
    \ "The man who is denied the opportunity of taking decisions of |
    `\ importance begins to regard as important the decisions he is |
    _o__) allowed to take." -- C. Northcote Parkinson |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Mark McEahern

      #3
      Re: regular expression question

      On Tue, 2004-01-20 at 18:38, Steve Lamb wrote:[color=blue]
      > Ok, this just seems convluted.
      >
      > import re
      > line = 'match this'
      > foo = re.search(r'.*( this)', line)
      > foo.groups()[0]
      >
      > To me there seems there should be a way to return a group without having
      > to get the tuple and then slice it out from there. However foo.group(0) will
      > return the entire string (only one argument given) and groups doesn't seem to
      > do anything significant with any entry I give it. Am I missing something
      > obvious or is this the final solution?[/color]

      Have you bothered to read the docs on Match objects?

      import re
      line = 'match this'
      match = re.search(r'.*( this)', line)
      print match.group(1)

      // m


      Comment

      • Steve Lamb

        #4
        Re: regular expression question

        On 2004-01-21, Ben Finney <bignose-hates-spam@and-benfinney-does-too.id.au>
        wrote:[color=blue]
        > On Wed, 21 Jan 2004 00:38:12 -0000, Steve Lamb wrote:[color=green]
        >> import re line = 'match this' foo = re.search(r'.*( this)', line)
        >> foo.groups()[0][/color][/color]
        [color=blue][color=green][color=darkred]
        > >>> import re line = 'match this' foo = re.search( r'.*(this)', line )
        > >>> foo.groups()[/color][/color]
        > ('this',)[color=green][color=darkred]
        > >>> foo.group(0)[/color][/color]
        > 'match this'[color=green][color=darkred]
        > >>> foo.group(1)[/color][/color]
        > 'this'[/color]
        [color=blue]
        > Read more on the methods available from a MatchObject:[/color]

        Fsck. Of course the groups are numbered from 1 but if one goes by the
        tuple returned they are numbered from 0. That was my mistake. Thanks for
        pointing it out to me. :P

        --
        Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
        PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
        -------------------------------+---------------------------------------------

        Comment

        Working...