regex Python正则表达式编译(使用re.VERBOSE)不工作

eyh26e7m  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(73)

我试图在编译正则表达式时加入注解,但当使用re.VERBOSE标志时,我再也得不到匹配结果了。
(使用Python 3.3.0)
使用前:

regex = re.compile(r"Duke wann", re.IGNORECASE)
print(regex.search("He is called: Duke WAnn.").group())

输出:Duke WAnn
之后:

regex = re.compile(r'''
Duke # First name 
Wann #Last Name
''', re.VERBOSE | re.IGNORECASE)

print(regex.search("He is called: Duke WAnn.").group())

输出量:

AttributeError: 'NoneType' object has no attribute 'group'
camsedfj

camsedfj1#

空格会被忽略(即,你的表达式实际上是DukeWann),所以你需要确保那里有一个空格:

regex = re.compile(r'''
Duke[ ] # First name followed by a space
Wann #Last Name
''', re.VERBOSE | re.IGNORECASE)

请访问http://docs.python.org/2/library/re.html#re.VERBOSE

相关问题