以下代码与多行字符串中的 foo2
不匹配:
re.match("^foo2", "foo1\nfoo2\nfoo3",re.MULTILINE)
这是为什么呢?文档位于 python.org表示在 MULTILINE
模式下,插入符 (^
) 也会匹配每行的开头。我也已经尝试过使用 r
的“原始字符串”版本,但没有任何乐趣。使用 findall
也没有帮助。
我显然在这里遗漏了一些东西..
最佳答案
参见re.match
文档:
Note that even in
MULTILINE
mode,re.match()
will only match at the beginning of the string and not at the beginning of each line.
您需要使用re.search
。
关于Python 正则表达式插入符号在多行模式下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32134396/