Search⌘ K
AI Features

Finding Multiple Instances

Explore how to locate multiple occurrences of patterns in a string using Python's regular expressions. Learn to use findall for lists of matches and finditer for iterators of match objects, enabling you to handle multiple instances effectively.

Up to this point, all we’ve seen is how to find the first match in a string. But what if we have a string that has multiple matches in it.

Let’s review how to find a single match:

Python 3.5
import re
silly_string = "the cat in the hat"
pattern = "the"
match = re.search(pattern, silly_string)
print (match.group())

Now we can see that there are ...