如果我执行 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/