Artem Artem - 5 months ago 6
Python Question

Why python re module does not work with @?

In the following example shows weird problem: Python pattern does not work.

import re
data = r'blah blah @Component blah blah'
m = re.match(r'\@Component', data)
print m


It would print out:

None


What did I miss here?

Answer

You need to use re.search instead, and @ has no special meaning so you do not need to escape it.

>>> re.search(r'@Component', data).group()
'@Component'