python - 使用 findall 捕获组?

标签 python regex

如果我执行 findall(r'regex(with)capturing.goes.here'),如何访问捕获的组? 我知道我可以通过 finditer 做到这一点,但我不想迭代。

最佳答案

findall 只返回捕获的组:

>>> re.findall('abc(de)fg(123)', 'abcdefg123 and again abcdefg123')
[('de', '123'), ('de', '123')]

相关文档摘录:

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.

关于python - 使用 findall 捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018340/

相关文章:

python - 正则表达式解析格式良好的多行数据字典

python - 日期列表中的时间序列-python

python - 从频率,和弦在 python 中播放流式声音

jquery,将部分文本输入解析为格式化的html输出?

java - 由于不需要的分隔符,字符串分割失败

java -\\s+ 和\s+ 有什么区别

Python:编辑分隔符之间的嵌套列表中的数字总和

python - 在 GLIBC 版本较低的目标系统上,从 .so 文件导入 Python 模块失败

regex - 如何仅通过更改域名来重定向 URL,同时保留其他 URL 参数

python - 正则表达式在 C++ 中不匹配?