2

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?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Artem
  • 7,275
  • 15
  • 57
  • 97
  • 1
    Given as the actual problem is in no way specific to `@`, perhaps the question's title should be changed so that it can better be found by folks to whom it's actually relevant? – Charles Duffy Sep 08 '14 at 19:32

2 Answers2

6

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'
hwnd
  • 69,796
  • 4
  • 95
  • 132
3

match tries to match the pattern at the beginning of the string. Use search instead. Also, @ doesn't have any special meaning in Python's regex, so you don't need to escape it.

Working code:

>>> re.search('(@Component)', data).groups()
('@Component',)
vaultah
  • 44,105
  • 12
  • 114
  • 143