user2803006
user2803006

Reputation:

regular expression in python *

I have a bit of problem with python regular expression first

import re

then I run a few test

>>> match = re.search(r'\w*i', "piiiiiiiiiiiiiiiiiiiip")
>>> print match.group()
>>>piiiiiiiiiiiiiiiiiiii


>>> match = re.search(r'i*i', "piiiiiiiiiiiiiiiiiiiip")
>>> print match.group()
>>>iiiiiiiiiiiiiiiiiiii

>>> match = re.search(r'i*', "iiiiiiiiiiiiiiiiiiiip")
>>> print match.group()
>>>iiiiiiiiiiiiiiiiiiii

>>>match = re.search(r'i*', "piiiiiiiiiiiiiiiiiiiig")
>>>print match.group()

>>> and got nothing

Do you guys know why the last one got nothing? I was expecting iiiiiiiiiiiiiiiiiiii as well.

Upvotes: 3

Views: 135

Answers (2)

Puffin GDI
Puffin GDI

Reputation: 1702

Because * is zero or more of the preceding element.

match = re.search(r'i+', "piiiiiiiiiiiiiiiiiiiig")
match.group()

Output:

'iiiiiiiiiiiiiiiiiiii'

Update

* is the equivalent of {0,}. When it find p, it satisfies requirement 0 i.

So it returns null character.

Upvotes: 5

glglgl
glglgl

Reputation: 91017

Despide regexes being "greedy" unless there is a ? at the right place, i* matches at the start - before the p - as there are 0 is. As this counts as a match, the search isn't continued.

Upvotes: 3

Related Questions