3

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I still don't really get it. 0 ~ max?
3

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.

1 Comment

Thanks for the explanation of why greediness doesn't affect the behavior here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.