python中的正则表达式错误
我有一个字符串,看起来像是一个路径,我正试图从中提取python中的正则表达式错误,python,Python,我有一个字符串,看起来像是一个路径,我正试图从中提取020414_001,其中包含一个正则表达式 它给了我这个错误: Traceback (most recent call last): File "test12.py", line 6, in <module> print m.group(0) AttributeError: 'NoneType' object has no attribute 'group' re.match从字符串开始匹配,改为尝试使用re.sea
020414_001
,其中包含一个正则表达式
它给了我这个错误:
Traceback (most recent call last):
File "test12.py", line 6, in <module>
print m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
re.match
从字符串开始匹配,改为尝试使用re.search
。此外,如果使用原始字符串r'C:\User\Test\…'
则不需要转义字符。您还需要将提供给re.compile
的字符串设置为原始字符串。否则python会将双反斜杠转义为单反斜杠。谢谢毛哲浩,我成功了。
str1 = <C:\\User\\Test\\xyz\\022014-101\\more\\stuff\\022014\\1>
import re
p = re.compile('(?<=\\)[\d]{6}[^\\]*')
m = p.match(str1)
print m.group(0) #Line 6
Traceback (most recent call last):
File "test12.py", line 6, in <module>
print m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
import re
m = re.search(r'(?<=\\)[\d]{6}[^\\]*', str1)
print m.group(0)