Regular Exp

Discussion in 'Python' started by afds, Oct 31, 2003.

  1. afds

    afds Guest

    If I want to match $.58 in $.589922 How do I do it with Regular Expressions?

    >>> s=re.search("\$\.[0-9]{2}", "$.589922")
    >>> s.string

    '$.589922'
    and not
    '$.58' which i s what I want
    I think it has something to do with Greediness right?
     
    afds, Oct 31, 2003
    #1
    1. Advertising

  2. afds

    Ben Finney Guest

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

    > '$.589922'


    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#l2h-875>

    > and not
    > '$.58' which i s what I want


    You need to define one or more groups within the expression, grouping
    what you want to access. Then use 'MatchObject.group()' to access one
    group at a time or 'MatchObject.groups()' to access all of them as a
    tuple:

    >>> s = re.search( "(\$\.[0-9]{2})", "$.589922" )
    >>> s.group(1)

    '$.58'
    >>> s.groups()

    ('$.58',)
    >>>


    --
    \ "I used to be a proofreader for a skywriting company." -- |
    `\ Steven Wright |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Oct 31, 2003
    #2
    1. Advertising

  3. afds

    Ben Finney Guest

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


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

    --
    \ "Any sufficiently advanced bug is indistinguishable from a |
    `\ feature." -- Rich Kulawiec |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Oct 31, 2003
    #3
  4. afds

    afds Guest

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

    "Ben Finney" <> wrote in
    message news:...
    > On 31 Oct 2003 19:29:14 +1050, Ben Finney wrote:
    > > You need to define one or more groups within the expression, grouping
    > > what you want to access. Then use 'MatchObject.group()' to access one

    >
    > Or, as Harvey pointed out, use 'MatchObject.group(0)' to access the
    > entire matching portion of the string, without needing to explicitly
    > define groups.
    >
    > --
    > \ "Any sufficiently advanced bug is indistinguishable from a |
    > `\ feature." -- Rich Kulawiec |
    > _o__) |
    > Ben Finney <http://bignose.squidly.org/>
     
    afds, Oct 31, 2003
    #4
  5. afds

    afds Guest

    Damn didn't see Harvey's message, ignore.

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

    for
    > two digits?
    >
    > "Ben Finney" <> wrote in
    > message news:...
    > > On 31 Oct 2003 19:29:14 +1050, Ben Finney wrote:
    > > > You need to define one or more groups within the expression, grouping
    > > > what you want to access. Then use 'MatchObject.group()' to access one

    > >
    > > Or, as Harvey pointed out, use 'MatchObject.group(0)' to access the
    > > entire matching portion of the string, without needing to explicitly
    > > define groups.
    > >
    > > --
    > > \ "Any sufficiently advanced bug is indistinguishable from a |
    > > `\ feature." -- Rich Kulawiec |
    > > _o__) |
    > > Ben Finney <http://bignose.squidly.org/>

    >
    >
     
    afds, Oct 31, 2003
    #5
    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. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,145
    Andrew Rowland
    Aug 2, 2003
  2. psk

    Newbie-Reg Exp

    psk, Jan 16, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,365
    Gunnar Hjalmarsson
    Jan 19, 2004
  3. Jayan Jacob
    Replies:
    4
    Views:
    99
    Sandor Szücs
    Jul 15, 2008
  4. stratus

    Multiple Match of regular exp...

    stratus, May 6, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    100
    John W. Krahn
    May 6, 2005
  5. Gandalf

    Variables in regular exp

    Gandalf, Oct 11, 2007, in forum: Javascript
    Replies:
    3
    Views:
    81
    Evertjan.
    Oct 11, 2007
Loading...

Share This Page