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


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

Updated on: 20-Feb-2020

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements