python - 正则表达式捕获一组的多个重复

标签 python regex

我有一个示例文本:

Lorem ipsum AB-CD-01 dolor sit amet, AB-CD-Foobar consectetur

我想捕获所有短语 AB-CD-*。我正在尝试类似的事情:

pattern = re.compile("((AB-CD-\S+).*)*")
result = pattern.search(text)
print(result.groups()) # expected: ('AB-CD-01', 'AB-CD-Foobar')

我知道这是相当简单和基本的正则表达式问题,但我找不到任何好的解决方案。

最佳答案

您可以使用更简单的模式 re.findall :

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

使用更新的正则表达式的示例代码:

import re
p = re.compile(r'AB-CD-\S+')
test_str = "Lorem ipsum AB-CD-01 dolor sit amet, AB-CD-Foobar consectetur"
print(re.findall(p, test_str))
# => ['AB-CD-01', 'AB-CD-Foobar']

参见IDEONEregex demo

re.search只查找第一个匹配项,re.findall 返回匹配列表(如果模式中没有定义捕获组 - 这就是我建议删除它们的原因)。

关于python - 正则表达式捕获一组的多个重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34895656/

相关文章:

Python 类型提示和上下文管理器

java - 用于排除控制字符并包含所有语言字符的正则表达式模式选项卡和新行必须包含

regex - Groovy 正则匹配引号之间的所有内容

java - 将倒数计时器转换为时间的正则表达式

php - REGEX 匹配除/以 : 开头的所有内容

c# - 删除除空格外的所有非数字字符

Python 诅咒破坏终端输出

python - 安装 Python 脚本,维护对 Python 2.6 的引用

python - 如何编写一个正则表达式来匹配一个字符串文字,其中转义是引号字符的两倍?

python - Pandas 和 Numpy 中的不同标准差