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']
Share:
376,717
kjakeb
Author by

kjakeb

Updated on July 18, 2022

Comments

  • kjakeb
    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
    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 a match_all or match_iter function.
  • Antoine Lizée
    Antoine Lizée over 7 years
    DISCLAIMER: those will find only non-overlapping matches
  • Raksha
    Raksha about 7 years
    @AntoineLizée, how does one find iterations WITH overlap?
  • ArtOfWarfare
    ArtOfWarfare almost 7 years
    @Raksha - Use re.search in a loop. It'll return a Match object. You'll want to pass in Match.start() + 1 as the pos argument for re.search for the next iteration of the loop.
  • rodorgas
    rodorgas almost 6 years
    If the match contains more than one group, findall will return a list of matching tuples, not a list of matching strings.
  • Fernando Wittmann
    Fernando Wittmann over 5 years
    Here'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
    nurettin about 5 years
    findall is not useful for named groups.