使用 findall 捕获组? [英] Capturing group with findall?

查看:61
本文介绍了使用 findall 捕获组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我执行 findall(r'regex(with)capturing.goes.here') ,我如何访问捕获的组?我知道我可以通过 finditer 来完成,但我不想重复.

解决方案

findall 只返回捕获的组:

<预><代码>>>>re.findall('abc(de)fg(123)', 'abcdefg123 和 abcdefg123')[('de', '123'), ('de', '123')]

相关文档摘录:

<块引用>

返回所有不重叠的匹配项字符串中的模式,作为列表字符串.字符串被扫描从左到右,匹配是按找到的顺序返回.如果一个或更多的团体出现在模式,返回组列表;这将是元组列表,如果模式有不止一组.空的匹配项包含在结果中除非他们触及另一场比赛.

How can I access captured groups if I do findall(r'regex(with)capturing.goes.here') ? I know I can do it through finditer, but I don't want to iterate.

解决方案

findall just returns the captured groups:

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

Relevant doc excerpt:

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.

这篇关于使用 findall 捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆