re.findall()
Published Jul 30, 2021Updated Jul 2, 2023
Contribute to Docs
The .findall()
method iterates over a string to find a subset of characters that match a specified pattern. It will return a list of every pattern match that occurs in a given string.
Syntax
re.findall(<pattern>, string)
Where <pattern>
can include any of the following:
- A string:
Jane
- A character class code:
/w
,/s
,/d
- A regex symbol:
$
,|
,^
There are optional arguments that include the following:
- A starting index value (pos):
3
- An index value to end the search (endpos):
40
- Flags:
IGNORECASE
,VERBOSE
,DOTALL
Example
Match all of the times in the string:
import removie_str = "the evening shows start at 7:00pm and 10:15pm"matches = re.findall(r"([\d:,.]+)(am|pm)?", movie_str)# Square brackets designate a custom character class# Parentheses identify a group within the patternprint(matches)
This will output the following:
[("7:00", "pm"), ("10:15", "pm")]
Codebyte Example
A regex to match all of the email addresses within a string:
All contributors
- CaupolicanDiaz
- BrandonDusch
- StevenSwiniarski
- christian.dinh
- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn the Basics of Regular Expressions
Get a taste of regular expressions (regex), a powerful search pattern language to quickly find the text you're looking for.Beginner Friendly1 hour