Python 正则表达式插入符号在多行模式下不起作用?

标签 python regex

以下代码与多行字符串中的 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/

相关文章:

Python:防止对字符串进行 %s 替换

java - @Pattern , JSR303 bean 验证 : regex check max 5 words and not blank

java - java中的分割词

python - 从 url 获取产品代码,我需要正则表达式吗?

javascript - 如何使用正则表达式在 javascript 中链接 unicode 数字

IDE 内和外的 Python Unicode

python - 属性错误 : 'Spotify' object has no attribute 'current_user_saved_tracks'

regex - Perl 使用定界符前缀拆分正则表达式

python - 排序元组 Python

python - 模板应该放在蓝图模板文件夹中还是全局模板文件夹中?