re.findall()
Anonymous contributor
Anonymous contributor92 total contributions
Anonymous contributor
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
- Anonymous contributorAnonymous contributor92 total contributions
- CaupolicanDiaz139 total contributions
- BrandonDusch580 total contributions
- StevenSwiniarski475 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributor
- CaupolicanDiaz
- BrandonDusch
- StevenSwiniarski
- christian.dinh
- Anonymous contributor
Looking to contribute?
- 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.