我对python还相当陌生,但已经使用regex一段时间了。我错过了什么:
>>> import re
>>> raceResuls = "2014 Results at:"
>>> raceDate = "Saturday, December 5, 2015"
>>> pattern = re.compile("(\d{4})")
>>> pattern.match(raceResuls).group(1)
'2014'
>>> pattern.match(raceDate).group(1)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
pattern.match(raceDate).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
为什么字符串开头匹配,但结尾不匹配?我在windows和linux上使用python2.7。你知道吗
您应该使用^{} 而不是^{} 。根据文件:
因此,当您使用} 相同(匹配字符串中第一个字符之前的位置)。你知道吗
match
时,它与在正则表达式中使用^{相关问题 更多 >
编程相关推荐