Odd behaviour of regexp module

D

David Veerasingam

Hello

It seems the grouping feature isn't behaving correctly.

In [1]: a = 'dfsf.oct.ocfe'

In [2]: b = re.match(r'^(.*?)\.', a); b.group()
'dfsf.'

The expected result is 'dfsf'. Why did the regexp grab that period at
the end?

David
 
M

Martin

Hi David,
b.group() is equivalent to b.group(0), the entire RE
match. (^(.*?)\.) will give you 'dfsf.' for that input string.

What you want is b.group(1), the subgroup you're looking for inside
the main RE. (.*?) which gives you 'dfsf', which is what you're
looking for.

Cheers,
Martin
 
D

Devan L

Why doesn't group have an argument? group() or group(0) returns what
the pattern matched, not what it returns.
 
N

ncf

To possibly clearify what the others have said, group() is the same as
saying group(0). Group is, if I recall correctly, supposed to return
the n-th subpattern from the regular expression (subpatterns, as you
know, being indicated by parenthises).

Hope that helps :)
-Wes
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top