How to find all adverbs and their positions in a text using python regular expression?

PythonServer Side ProgrammingProgramming

As per Python documentation

If one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −

>>> 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
raja
Published on 11-Jan-2018 13:23:49
Advertisements