Categories
Python Answers

How to find all matches to a regular expression in Python?

Spread the love

Sometimes, we want to find all matches to a regular expression in Python.

In this article, we’ll look at how to find all matches to a regular expression in Python.

How to find all matches to a regular expression in Python?

To find all matches to a regular expression in Python, we can use the re.findall method.

For instance, we write

m = re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all cats are dogs')

to call re.findall to search for all matches of words between ‘all’ and ‘are’ by calling it with r'all (.*?) are' and 'all cats are smarter than dogs, all cats are dogs'.

Conclusion

To find all matches to a regular expression in Python, we can use the re.findall method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *