0

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?

4

1 回答 1

3

美元符号是正则表达式中的一个特殊字符,意思是“行尾”。你需要逃避它:

b = r'c\$}on(.*)tent(.*)mo(.*)re'
于 2013-06-18T21:34:18.560 回答