python - 为什么我的 RegEx 在 RegEx 中匹配,但在 Python 中不匹配?

标签 python regex

<分区>

regexp = re.compile('[A-Z]\\d{4}')
prefix = regexp.match("\"O1533_FOO\" INTEGER NOT NULL ,")

前缀是无,但我预计它是“O1533”。

当我使用基于网络的工具时 http://regexr.com/它有效。

http://regexr.com/3bag5

我试图用不同数量的 \ 转义 \,我还尝试分隔 /。无法让它工作。

RegExp 专家可以修正我的表达式吗?

最佳答案

因为您应该将 match 替换为 search

match 尝试从头开始匹配字符串,而 \"O1533_FOO\"INTEGER NOT NULL , 不以匹配 [A-Z 的内容开头],因此它失败了。

参见 search() vs. match() :

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

关于python - 为什么我的 RegEx 在 RegEx 中匹配,但在 Python 中不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165137/

相关文章:

python - Django:测试应用程序导致 Tastypie 错误

java - 匹配(基本)函数声明

Python根据条件拆分字符串

python Pandas : exclude rows below a certain frequency count

python - 如何使用 py2neo 声明唯一约束

python - 在Keras中重置模型的所有权重和偏差(训练后恢复模型)

java - 由空格分隔的 9 个唯一数字的正则表达式?

python - Python 中的 %i 和 %d 有什么区别?

c# - 我的正则表达式有什么问题

c++ - 为什么使用 "cmatch"的 C++ 正则表达式代码会引发 "smatch"异常?