showkey
showkey

Reputation: 358

the meaning of * (zero time or many times) in the python regular expression

Why print(re.search("c*","bccc").group()) get nothing ? I think it will get ccc too, * is zero time or many times ,c* can match ccc,why can't get ccc in the expressionre.search("c*","bccc")?

>>> print(re.search("c*","ccc").group())  
ccc  
>>> print(re.search("c*","bccc").group())  

Upvotes: 2

Views: 284

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121634

Regular expressions scan from the start, and re.search() returns the first legal match. Because you asked for zero or more c characters, the empty string is the first legal match.

Either use re.findall() to find all non-overlapping legal matches:

>>> re.findall("c*","bccc")
['', 'ccc', '']

or add an anchor:

>>> re.search("c*$","bccc").group()
'ccc'

or use + to match 1 or more characters:

>>> re.search("c+","bccc").group()
'ccc'

Upvotes: 4

Related Questions