Regular Exp

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

    Regular Exp

    If I want to match $.58 in $.589922 How do I do it with Regular Expressions?
    [color=blue][color=green][color=darkred]
    >>> s=re.search("\$ \.[0-9]{2}", "$.589922")
    >>> s.string[/color][/color][/color]
    '$.589922'
    and not
    '$.58' which i s what I want
    I think it has something to do with Greediness right?


  • Ben Finney

    #2
    Re: Regular Exp

    On Fri, 31 Oct 2003 08:11:39 GMT, afds wrote:[color=blue]
    > If I want to match $.58 in $.589922 How do I do it with Regular Expressions?
    >[color=green][color=darkred]
    > >>> s=re.search("\$ \.[0-9]{2}", "$.589922")
    > >>> s.string[/color][/color]
    > '$.589922'[/color]

    Yes. The 'string' member of a MatchObject returns the entire string
    that was passed to the search() that produced it, as the docs will tell
    you:

    <http://www.python.org/doc/current/lib/match-objects.html#l2 h-875>
    [color=blue]
    > and not
    > '$.58' which i s what I want[/color]

    You need to define one or more groups within the expression, grouping
    what you want to access. Then use 'MatchObject.gr oup()' to access one
    group at a time or 'MatchObject.gr oups()' to access all of them as a
    tuple:
    [color=blue][color=green][color=darkred]
    >>> s = re.search( "(\$\.[0-9]{2})", "$.589922" )
    >>> s.group(1)[/color][/color][/color]
    '$.58'[color=blue][color=green][color=darkred]
    >>> s.groups()[/color][/color][/color]
    ('$.58',)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    --
    \ "I used to be a proofreader for a skywriting company." -- |
    `\ Steven Wright |
    _o__) |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Ben Finney

      #3
      Re: Regular Exp

      On 31 Oct 2003 19:29:14 +1050, Ben Finney wrote:[color=blue]
      > You need to define one or more groups within the expression, grouping
      > what you want to access. Then use 'MatchObject.gr oup()' to access one[/color]

      Or, as Harvey pointed out, use 'MatchObject.gr oup(0)' to access the
      entire matching portion of the string, without needing to explicitly
      define groups.

      --
      \ "Any sufficiently advanced bug is indistinguishab le from a |
      `\ feature." -- Rich Kulawiec |
      _o__) |
      Ben Finney <http://bignose.squidly .org/>

      Comment

      • afds

        #4
        Re: Regular Exp

        Ah, so you're saying my Regular Expression is correct? It just matches for
        two digits?

        "Ben Finney" <bignose-hates-spam@and-benfinney-does-too.id.au> wrote in
        message news:slrnbq49du .107.bignose-hates-spam@rose.local domain.fake...[color=blue]
        > On 31 Oct 2003 19:29:14 +1050, Ben Finney wrote:[color=green]
        > > You need to define one or more groups within the expression, grouping
        > > what you want to access. Then use 'MatchObject.gr oup()' to access one[/color]
        >
        > Or, as Harvey pointed out, use 'MatchObject.gr oup(0)' to access the
        > entire matching portion of the string, without needing to explicitly
        > define groups.
        >
        > --
        > \ "Any sufficiently advanced bug is indistinguishab le from a |
        > `\ feature." -- Rich Kulawiec |
        > _o__) |
        > Ben Finney <http://bignose.squidly .org/>[/color]


        Comment

        • afds

          #5
          Re: Regular Exp

          Damn didn't see Harvey's message, ignore.
          [color=blue]
          > Ah, so you're saying my Regular Expression is correct? It just matches[/color]
          for[color=blue]
          > two digits?
          >
          > "Ben Finney" <bignose-hates-spam@and-benfinney-does-too.id.au> wrote in
          > message news:slrnbq49du .107.bignose-hates-spam@rose.local domain.fake...[color=green]
          > > On 31 Oct 2003 19:29:14 +1050, Ben Finney wrote:[color=darkred]
          > > > You need to define one or more groups within the expression, grouping
          > > > what you want to access. Then use 'MatchObject.gr oup()' to access one[/color]
          > >
          > > Or, as Harvey pointed out, use 'MatchObject.gr oup(0)' to access the
          > > entire matching portion of the string, without needing to explicitly
          > > define groups.
          > >
          > > --
          > > \ "Any sufficiently advanced bug is indistinguishab le from a |
          > > `\ feature." -- Rich Kulawiec |
          > > _o__) |
          > > Ben Finney <http://bignose.squidly .org/>[/color]
          >
          >[/color]


          Comment

          Working...