Ryan Saxe
Ryan Saxe

Reputation: 17829

regex search is not catching what it should

So I have to get part of a known string string. I use re.search for this. But in this specific circumstance, it is not catching what it should:

>>> a = 'c$}ononetentonemotw{$ore'
>>> b = 'c$}on(.*)tent(.*)mo(.*)re'
>>> c = re.search(b,a)
>>> c.groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'

c.groups() should return: ('one','one','tw{$o'), yet it does not actually catch this pattern, Why?

Upvotes: 0

Views: 50

Answers (1)

BrenBarn
BrenBarn

Reputation: 251355

The dollar sign is a special character in regular expressions, meaning "end of line". You need to escape it:

b = r'c\$}on(.*)tent(.*)mo(.*)re'

Upvotes: 3

Related Questions