Regular Exp

B

Ben Finney

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:

said:
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',)
>>>
 
B

Ben Finney

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.
 
A

afds

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top