As the previous answers states, using *
regex expression matches 0 or more matches. That's the tricky part.
You can do some testing, and better understand how Python treats *
. The match object has the groups() method which returns the matches in groups:
>>> r.match(str_).groups()
()
This seems odd. How come this returns an empty group? This is even more strange: the findall method returns a list of matched items
>>> r.findall(str_)
['', '']
Two empty strings. This means even when there is no match, you'll get an empty item (list / tuple). If you change the *
to +
(match one or more) the results are different:
>>> pattern = '[^ab]+'
>>> re.match(pattern, 'a').groups()
Traceback (most recent call last):
File "<pyshell#135>", line 1, in <module>
re.match(pattern, 'a').groups()
AttributeError: 'NoneType' object has no attribute 'groups'
This returns None
.
So, to sum it up, Python treats *
with additional empty string at the beginning of the searched string when used in pattern such []*
. However, when the pattern []+
is used it doesn't do that.