python - 如何强制 Python RegEx 匹配由 | 连接的所有可能组 (*或者*)
问题描述
我是 RegEx 的新手,我想知道是否有一种方法可以强制 RegEx 在同一个“匹配”中匹配所有可能的组(如果有多个组),其中模式通过OR连接(见下文)。
我试过这个:(?P<broad>travel)|(?P<step>step)|(?P<dist>distance|far|km)
,但如果输入是:Tell me how many steps I traveled
,代码只匹配旅行或步骤之一。我也尝试过使用findall
而不是search
,但是组信息丢失了(因为输出是一个列表)。
我希望代码可以匹配同一“匹配”中的所有可能组(如果可用),而不是在找到匹配项后立即退出。
电流输出:
Match 1
broad None
step step
dist None
Match 2
broad travel
step None
dist None
预期输出:
Match 1
broad travel
step step
dist None
解决方案
也许在这里,我们可以使用finditer
和测试我们的表达式:
演示
测试
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(travel)|(step)|(distance|far|km)"
test_str = "Tell me how many steps I traveled"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
演示
const regex = /(travel)|(step)|(distance|far|km)/gm;
const str = `Tell me how many steps I traveled`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
推荐阅读
- javascript - 如何在 ExtJs 中集成 java api
- arrays - 无法使用 Angular 插值显示 JSON 数据
- matlab - 在MATLAB中查找一行中唯一零的列号
- python - 停止 sqlalchemy 管理连接
- javascript - 执行简单的脚本,但控制台警告 ~= 'getComputedStyle' on 'Window': parameter is not of type 'Element'
- c# - UWP 应用程序后台任务在鼠标/键盘断开连接时停止
- r - 获取每月的第 2 个和第 4 个星期六
- azure - 从 Azure 服务总线在 Azure Web 作业中多次收到相同的消息
- angular - 在 Angular 7 中测试 http 404 错误响应
- django-forms - Django textarea 小部件没有属性“input_type”