【发布时间】:2012-12-07 10:58:41
【问题描述】:
我试图在编译正则表达式时加入 cmets,但在使用 re.VERBOSE 标志时,我不再得到匹配结果。
(使用 Python 3.3.0)
之前:
regex = re.compile(r"Duke wann", re.IGNORECASE)
print(regex.search("He is called: Duke WAnn.").group())
输出:杜克·万恩
之后:
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”对象没有属性“组”
【问题讨论】:
-
实际上这是原始多行字符串的错误语法:
r'''this is wrong'''。正确的语法必须使用带双引号的 r:r"""this is right"""。见How to correctly write a raw multiline string in Python?