14 Python Regex Finditer Function
14 Python Regex Finditer Function
Syntax #
Here the string is scanned left-to-right, and matches are returned in the order
found. Empty matches are included in the result unless they touch the
beginning of another match.
Example 1 #
Here is a simple example which demonstrates the use of finditer. It reads in a
page of html text, finds all the occurrences of the word “the” and prints “the”
and the following word. It also prints the character position of each match
using the MatchObject’s start() method.
import re
import urllib2
html = urllib2.urlopen('https://docs.python.org/2/library/re.html').read()
pattern = r'\b(the\s+\w+)\s+'
regex = re.compile(pattern, re.IGNORECASE)
for match in regex.finditer(html):
print "%s: %s" % (match.start(), match.group(1))
Once you have the list of tuples, you can loop over it to do some computation
for each tuple.
Expected output:
output