如何使用 Python 正则表达式查找文本中的所有副词及其位置?
pythonserver side programmingprogramming更新于 2025/7/9 13:32:17
根据 Python 文档
如果想要有关模式的所有匹配项的更多信息而不是匹配的文本,finditer() 很有用,因为它提供匹配对象而不是字符串。如果作者想要在某些文本中找到所有副词及其位置,他或她将以以下方式使用 finditer() −
>>> text = "He was closely disguised but caught soon by police."
>>> for m in re.finditer(r"\w+ly", text):
... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
07-16: 小心
40-47: 迅速
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

