How can I find all matches to a regular expression in Python?
376,717
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( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
# Output: ['cats', 'dogs']
[x.group() for x in re.finditer( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')]
# Output: ['all cats are', 'all dogs are']
Author by
kjakeb
Updated on July 18, 2022Comments
-
kjakeb almost 2 years
In a program I'm writing I have Python use the
re.search()
function to find matches in a block of text and print the results. However, the program exits once it finds the first match in the block of text.How do I do this repeatedly where the program doesn't stop until ALL matches have been found? Is there a separate function to do this?
-
dsclose over 8 years
finditer
was what I was looking for. I'm surprised that one returns Match objects and the other strings. I was expecting to use amatch_all
ormatch_iter
function. -
Antoine Lizée over 7 yearsDISCLAIMER: those will find only non-overlapping matches
-
Raksha about 7 years@AntoineLizée, how does one find iterations WITH overlap?
-
ArtOfWarfare almost 7 years@Raksha - Use
re.search
in a loop. It'll return aMatch
object. You'll want to pass inMatch.start() + 1
as thepos
argument forre.search
for the next iteration of the loop. -
rodorgas almost 6 yearsIf the match contains more than one group,
findall
will return a list of matching tuples, not a list of matching strings. -
Fernando Wittmann over 5 yearsHere's an example to help visualizing:
re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
(output:['cats', 'dogs']
) -
nurettin about 5 yearsfindall is not useful for named groups.