如何使用 Python 正则表达式查找文本中的所有副词及其位置?
根据 Python 文档
如果需要关于模式所有匹配的更多信息,而不是匹配的文本,finditer() 很有用,因为它提供匹配对象而不是字符串。如果某个写作者想要查找某个文本中的所有副词及其位置,他或她将以以下方式使用 finditer() -
>>> 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
广告