Regular Expression
findall() matches all occurrences of a pattern, not just the first one as search() does. For example, if a writer wanted to find all of the adverbs in some text, they might use findall() in the following manner:
>>> text = "He was carefully disguised but captured quickly by police."
>>> re.findall(r"\w+ly", text)
['carefully', 'quickly']
也可以采用如下方式:
pattern = re.compile(r’regular_expression’)
pattern.findall(text)
finditer() get more information about all matches of a pattern
>>> text = "He was carefully disguised but captured quickly by police."
>>> for m in re.finditer(r"\w+ly", text):
... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
07-16: carefully
40-47: quickly

本文深入探讨Python中正则表达式的使用技巧,通过实例演示如何利用findall()和finditer()方法查找文本中所有匹配项,如在句子中找出所有副词。这不仅展示了正则表达式的强大功能,还提供了实际操作的代码示例。
1万+

被折叠的 条评论
为什么被折叠?



