Python re Module
Example
Find all email addresses in a text:
import re
text = 'Contact Emil at emil@example.com or Tobias at tobias@test.com'
emails = re.findall(r'\S+@\S+', text)
print(emails)
Try it Yourself »
Definition and Usage
The re module provides regular expression operations for pattern matching in strings.
Use it to search, match, split, and replace text based on patterns, validate input formats, or extract specific data from strings.
Members
Member | Description |
---|---|
A | Alias for ASCII flag. |
ASCII | Make \w, \b, \s match ASCII characters only. |
compile() | Compile a regular expression pattern into a Pattern object. |
DEBUG | Display debug information about compiled expression. |
DOTALL | Make . match any character including newline. |
escape() | Escape special characters in a string for use in a regex pattern. |
findall() | Return all non-overlapping matches as a list of strings. |
finditer() | Return an iterator yielding Match objects for all matches. |
fullmatch() | Match the entire string against a pattern. |
I | Alias for IGNORECASE flag. |
IGNORECASE | Perform case-insensitive matching. |
L | Alias for LOCALE flag. |
LOCALE | Make \w, \b, \s depend on current locale. |
M | Alias for MULTILINE flag. |
Match | Type returned by successful match() and search() calls. |
match() | Match a pattern at the beginning of a string. |
MULTILINE | Make ^ match start of each line and $ match end of each line. |
Pattern | Type for compiled regular expression objects. |
purge() | Clear the regular expression cache. |
RegexFlag | Enum for regular expression flags. |
S | Alias for DOTALL flag. |
search() | Search for the first location where a pattern matches. |
split() | Split a string by occurrences of a pattern. |
sub() | Replace occurrences of a pattern with a replacement string. |
subn() | Replace occurrences and return (new_string, number_of_subs_made). |
template() | Compile a template pattern (deprecated). |
U | Alias for UNICODE flag. |
UNICODE | Make \w, \b, \s match Unicode characters (Python 2 compatibility). |
VERBOSE | Allow verbose regular expressions with whitespace and comments. |
X | Alias for VERBOSE flag. |