Python re Module
Python’s re module is a standard library module for handling regular expressions.
Regular expressions (regex or regexp) are powerful tools for matching, searching, and manipulating text.
Through the re module, you can use regular expressions to process strings in Python.
Why Use the re Module?
Section titled “Why Use the re Module?”When processing text, we often need to find specific patterns or replace certain characters. For example, validating email addresses, extracting links from web pages, or formatting text. Writing code manually to accomplish these tasks can be very tedious, while regular expressions provide a concise and efficient way to solve these problems.
Basic Usage of the re Module
Section titled “Basic Usage of the re Module”1. Importing the re Module
Section titled “1. Importing the re Module”Before using the re module, you first need to import it:
2. Common re Module Functions
Section titled “2. Common re Module Functions”2.1 re.match()
Section titled “2.1 re.match()”The re.match() function matches a regular expression from the beginning of a string. If the match is successful, it returns a match object; otherwise, it returns None.
Example
Section titled “Example”Output:
2.2 re.search()
Section titled “2.2 re.search()”The re.search() function searches for the first match of a regular expression in a string. Unlike re.match(), re.search() does not require the match to start from the beginning of the string.
Example
Section titled “Example”Output:
2.3 re.findall()
Section titled “2.3 re.findall()”The re.findall() function finds all substrings in a string that match the regular expression and returns a list.
Example
Section titled “Example”Output:
2.4 re.sub()
Section titled “2.4 re.sub()”The re.sub() function replaces parts of a string that match the regular expression.
Example
Section titled “Example”Output:
Basic Syntax of Regular Expressions
Section titled “Basic Syntax of Regular Expressions”1. Regular Characters
Section titled “1. Regular Characters”Regular characters (such as letters, numbers) match themselves directly.
Example
Section titled “Example”Output:
2. Special Characters
Section titled “2. Special Characters”Regular expressions have some special characters with special meanings. For example:
.: Matches any single character (except newline).*: Matches the preceding character zero or more times.+: Matches the preceding character one or more times.?: Matches the preceding character zero or one time.\d: Matches any digit character (equivalent to[0-9]).\w: Matches any letter, digit, or underscore character (equivalent to[a-zA-Z0-9_]).
Example
Section titled “Example”Output:
3. Character Sets
Section titled “3. Character Sets”Character sets are used to match any one character from a group of characters. For example, [abc] matches a, b, or c.
Example
Section titled “Example”Output:
4. Grouping
Section titled “4. Grouping”Grouping allows you to combine multiple characters together and perform operations on them. For example, (abc) matches abc.
Example
Section titled “Example”Output:
Practice Exercises
Section titled “Practice Exercises”Exercise 1: Validate an Email Address
Section titled “Exercise 1: Validate an Email Address”Write a regular expression to validate the format of an email address.
Example
Section titled “Example”Exercise 2: Extract a Phone Number
Section titled “Exercise 2: Extract a Phone Number”Write a regular expression to extract a phone number from text.
Example
Section titled “Example”1. Core Functions
Section titled “1. Core Functions”| Method | Description | Example |
|---|---|---|
re.compile(pattern) |
Precompile a regular expression (improves reuse performance) | pat = re.compile(r'\d+') |
re.search(pattern, string) |
Search for the first match in a string | re.search(r'\d+', 'a1b2') → matches '1' |
re.match(pattern, string) |
Match from the beginning of the string | re.match(r'\d+', '123a') → matches '123' |
re.fullmatch(pattern, string) |
Match the entire string completely | re.fullmatch(r'\d+', '123') → matches '123' |
re.findall(pattern, string) |
Return a list of all non-overlapping matches | re.findall(r'\d+', 'a1b22c') → ['1', '22'] |
re.finditer(pattern, string) |
Return an iterator of all matches (with position info) | for m in re.finditer(r'\d+', 'a1b2'): print(m.group()) |
re.sub(pattern, repl, string) |
Replace matches | re.sub(r'\d+', 'X', 'a1b2') → 'aXbX' |
re.split(pattern, string) |
Split a string by matches | re.split(r'\d+', 'a1b2c') → ['a', 'b', 'c'] |
re.escape() |
Escape special characters | re.escape("C:\\Users\\test.txt") |
re.purge() |
Clear the cache | re.purge() |
2. Match Object (Match) Methods/Attributes
Section titled “2. Match Object (Match) Methods/Attributes”| Method/Attribute | Description | Example |
|---|---|---|
group() |
Return the entire matched string | m.group() → 'abc' |
group(n) |
Return the content of the nth capture group | m = re.search(r'(\d)(\d)', '12'); m.group(1) → '1' |
groups() |
Return a tuple of all capture groups | m.groups() → ('1', '2') |
start()/end() |
Start/end position of the match | m.start() → 0 |
span() |
Return the match range (start, end) |
m.span() → (0, 2) |
3. Regular Expression Metacharacters (Partial)
Section titled “3. Regular Expression Metacharacters (Partial)”| Metacharacter | Description | Example Match |
|---|---|---|
. |
Matches any character (except newline) | a.c → 'abc' |
\d |
Matches digits | \d+ → '123' |
\D |
Matches non-digits | \D+ → 'abc' |
\w |
Matches word characters (letters, digits, underscore) | \w+ → 'Ab_1' |
\W |
Matches non-word characters | \W+ → '!@#' |
\s |
Matches whitespace characters (space, tab, etc.) | \s+ → ' \t' |
\S |
Matches non-whitespace characters | \S+ → 'abc' |
[] |
Character set | [A-Za-z] → any letter |
^ |
Matches the beginning of a string | ^\d+ → digits at the start |
$ |
Matches the end of a string | \d+$ → digits at the end |
* |
Matches the preceding character 0 or more times | a* → '', 'aaa' |
+ |
Matches the preceding character 1 or more times | a+ → 'a', 'aaa' |
? |
Matches the preceding character 0 or 1 time | a? → '', 'a' |
{m,n} |
Matches the preceding character m to n times | a{2,3} → 'aa', 'aaa' |
| **` | `** | OR operation |
() |
Capture group | (\d+) → extract digits |
4. Compilation Flags (flags Parameter)
Section titled “4. Compilation Flags (flags Parameter)”| Flag | Description | Example |
|---|---|---|
re.IGNORECASE (re.I) |
Ignore case | re.search(r'abc', 'ABC', re.I) |
re.MULTILINE (re.M) |
Multi-line mode (affects ^ and $) |
re.findall(r'^\d+', '1\n2', re.M) → ['1', '2'] |
re.DOTALL (re.S) |
Make . match all characters including newline |
re.search(r'a.*b', 'a\nb', re.S) |
re.ASCII |
Make \w, \W, etc. match only ASCII characters |
re.search(r'\w+', 'こん', re.ASCII) → no match |
re.VERBOSE (re.X) |
Allow comments and whitespace in regex | re.compile(r'''\d+ # match digits''', re.X) |
Examples
Section titled “Examples”1. Extracting Email Addresses
Example
Section titled “Example”2. Replacing Date Format
Example
Section titled “Example”3. Multi-Condition Matching
Example
Section titled “Example”4. Splitting Complex Strings
Example
Section titled “Example”Important Notes
Section titled “Important Notes”-
Raw Strings: It is recommended to use raw strings for regular expressions (e.g.,
r'\d') to avoid escape character conflicts. -
Greedy Matching: By default, matching is greedy (e.g.,
.*matches the longest possible). Use.*?for non-greedy matching. -
Performance Optimization: Frequently used regex should be precompiled with
re.compile(). -
Backtracking Issues: Complex regex can lead to performance problems (e.g., nested quantifiers like
(a+)+).