python - 正则表达式中的 `\A` 与 `^`(插入符号)之间是否存在差异?

标签 python regex python-2.7

Python 的 re module documentation说:

^: (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.

\A: Matches only at the start of the string.

使用时有什么区别吗?

最佳答案

这两个匹配:

re.search('^abc', 'abc')
re.search('\Aabc', 'abc')

这也匹配:

re.search('^abc', 'firstline\nabc', re.M)

这不是:

re.search('\Aabc', 'firstline\nabc', re.M)

关于python - 正则表达式中的 `\A` 与 `^`(插入符号)之间是否存在差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669147/

相关文章:

python - 如何从 Python 中的 NIC(网络接口(interface) Controller )获取 IP 地址?

javascript - 用于匹配/替换 JavaScript 注释的正则表达式(多行和内联)

Python快速从单个numpy数组中的多个文件获取数据的方法

regex - 正则表达式 - 匹配空格

java - 正则表达式替换 Spring @RequestMapping URL 中的 @PathVariable 值

Python 属性实例行为

python - 如何从 html 表格行中提取文本

python - 如何在 pandas 数据框中保留每组的前 [2-31] 行?

python - 使用 Python 检查任意用户是否在管理员组中

python:读取和搜索 .txt 文件