我在 python 中遇到正则表达式匹配问题,我有一个字符串如下:
test_str = ("ICD : 12123575.007787. 098.3,\n"
"193235.1, 132534.0, 17707.1,1777029, V40‚0, 5612356,9899\n")
我的正则表达式有两个主要组与 |
绑定(bind)在一起该正则表达式如下:
regex = r"((?<=ICD\s:\s).*\n.*)|((?<=ICD\s).*)"
让我们称他们为(A | B)
.在哪里A = ((?<=ICD\s:\s).*\n.*)
和 B = ((?<=ICD\s).*)
.根据documentation |
以 if A
的方式工作匹配它不会进一步与 B
.
现在我的问题是,当我使用上面提到的正则表达式时 test_str
.它匹配 B
但不适用于 A
.但是如果我用正则表达式搜索 A
只有 (即 ((?<=ICD\s:\s).*\n.*)
),然后是 test_string
与正则表达式 A
匹配.所以我的问题是为什么使用 A|B
正则表达式与组 A
不匹配停了下来。以下是我的 python 代码:
import re
regex = r"((?<=ICD\s:\s).*\n.*)|((?<=ICD\s).*)"
test_str = ("ICD : 12123575.007787. 098.3,\n"
"193235.1, 132534.0, 17707.1,1777029, V40‚0, 5612356,9899\n")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(
start = matches.start(),
end = matches.end(),
match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(
groupNum = groupNum,
start = matches.start(groupNum),
end = matches.end(groupNum),
group = matches.group(groupNum)))
输出:
Match was found at 4-29: : 12123575.007787. 098.3,
Group 1 found at -1--1: None
Group 2 found at 4-29: : 12123575.007787. 098.3,
如果您无法理解,请见谅。我不知道为什么Group 1 found at -1--1: None
不匹配。如果您理解,请告诉我可能是什么原因。
最佳答案
之所以会出现这种情况是因为正则表达式从左到右搜索匹配项,而正则表达式的右半部分更早匹配。这是因为左边的表达式有更长的回溯:(?<=ICD\s:\s)
需要比 (?<=ICD\s)
多两个字符.
test_str = "ICD : 12123575.007787. 098.3,\n"
# ^ left half of the regex matches here
# ^ right half of the regex matches here
换句话说,您的正则表达式本质上类似于 (?<=.{3})
和 (?<=.)
.如果你试过 re.search(r'(?<=.{3})|(?<=.)', some_text)
,很明显,正则表达式的右侧将首先匹配,因为它的后视更短。
您可以通过添加否定先行来防止正则表达式的右半部分过早匹配来解决此问题:
regex = r"((?<=ICD\s:\s).*\n.*)|((?<=ICD\s)(?!:\s).*)"
# ^^^^^^^
test_str = "ICD : 12123575.007787. 098.3,\n"
# ^ left half of the regex matches here
# right half of the regex matches doesn't match at all
关于python - 正则表达式在使用 OR 运算符匹配第一条规则后不会停止评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45800747/