regex – How can I find all matches to a regular expression in Python?
regex – How can I find all matches to a regular expression in Python?
Use re.findall
or re.finditer
instead.
re.findall(pattern, string)
returns a list of matching strings.
re.finditer(pattern, string)
returns an iterator over MatchObject
objects.
Example:
re.findall( rall (.*?) are, all cats are smarter than dogs, all dogs are dumber than cats)
# Output: [cats, dogs]
[x.group() for x in re.finditer( rall (.*?) are, all cats are smarter than dogs, all dogs are dumber than cats)]
# Output: [all cats are, all dogs are]