python - 正则表达式 : How to access multiple matches of a group?

标签 python regex

我正在组合一个相当复杂的正则表达式。表达式的一部分匹配诸如“+a”、“-57”等字符串。A + 或a - 后跟任意数量的字母或数字。我想匹配 0 个或多个匹配此模式的字符串。

这是我想出的表达方式:

([\+-][a-zA-Z0-9]+)*

如果我要使用此模式搜索字符串“-56+a”,我希望得到两个匹配项:

+a 和 -56

但是,我只返回最后一个匹配项:

>>> m = re.match("([\+-][a-zA-Z0-9]+)*", '-56+a')
>>> m.groups()
('+a',)

查看 python 文档,我看到:

If a group matches multiple times, only the last match is accessible:

>>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
>>> m.group(1)                        # Returns only the last match.
'c3'

所以,我的问题是:如何您访问多个小组比赛?

最佳答案

从您的正则表达式中删除 * (因此它与您的模式的一个实例完全匹配)。然后使用 re.findall(...)re.finditer (参见 here )返回所有匹配项。

更新:

听起来您实际上是在构建 recursive descent parser .对于相对简单的解析任务,手动完成是很常见且完全合理的。如果您对库解决方案感兴趣(例如,如果您的解析任务稍后可能变得更加复杂),请查看 pyparsing .

关于python - 正则表达式 : How to access multiple matches of a group?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5060659/

相关文章:

regex - Logwatch ignore.conf 的多行正则表达式

python - 填充对称二维数组

python - 将 pytorch 数据加载器加载到 GPU

python - 这是将两个字典的字典合并在一起并对相似值求和的最佳方法吗

Java 正则表达式 : Count words/phrases/wildcards in HTML document

java - Maven jarsigner,所有以 "test"开头的 jar

python - 入门 - Spark, IPython notebook with pyspark

python - 附加对于具有非多部分负载的消息无效

用于 R 注释的正则表达式 Emacs

php - Regex Youtube-将自动播放设置为0(特定视频哈希除外)