<分区>
regexp = re.compile('[A-Z]\\d{4}')
prefix = regexp.match("\"O1533_FOO\" INTEGER NOT NULL ,")
前缀是无,但我预计它是“O1533”。
当我使用基于网络的工具时 http://regexr.com/它有效。
我试图用不同数量的 \
转义 \
,我还尝试分隔 /
。无法让它工作。
RegExp 专家可以修正我的表达式吗?
<分区>
regexp = re.compile('[A-Z]\\d{4}')
prefix = regexp.match("\"O1533_FOO\" INTEGER NOT NULL ,")
前缀是无,但我预计它是“O1533”。
当我使用基于网络的工具时 http://regexr.com/它有效。
我试图用不同数量的 \
转义 \
,我还尝试分隔 /
。无法让它工作。
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, whilere.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/