Rayer
Rayer

Reputation: 67

Regular Expression in Python can't parse string contains dot

I got a problem about Python Regex.

>>> import re
>>> print re.match('img', 'test.img')
None
>>> print re.match('test', 'test.img')
<_sre.SRE_Match object at 0x7f3fac8a0100>
>>>

Any character after dot(.) won't be parsed, is there any way to solve this problem?

Upvotes: 0

Views: 201

Answers (1)

falsetru
falsetru

Reputation: 369134

re.match matches only at the beginning of the string. Use search instead. (See search() vs. match())

>>> import re
>>> re.match('img', 'test.img')
>>> re.search('img', 'test.img')
<_sre.SRE_Match object at 0x0000000002AB0100>

Upvotes: 7

Related Questions