Python3 Regular Expressions
A regular expression is a special sequence of characters that helps you easily check whether a string matches a certain pattern.
In Python, the re module is used to handle regular expressions.
The re module provides a set of functions that allow you to perform pattern matching, searching, and replacement operations on strings.
The re module gives the Python language complete regular expression functionality.
This chapter mainly introduces commonly used regular expression processing functions in Python. If you are not familiar with regular expressions, you can check our Regular Expression - Tutorial.
re.match Function
Section titled “re.match Function”re.match attempts to match a pattern from the starting position of a string. If the match is not successful at the starting position, match() returns None.
Function syntax:
Function parameter description:
| Parameter | Description |
| pattern | The regular expression pattern to match |
| string | The string to match. |
| flags | Flags to control the matching behavior of the regular expression, such as case sensitivity, multiline matching, etc. See: Regular Expression Modifiers - Optional Flags |
On successful match, re.match returns a match object; otherwise it returns None.
We can use the group(num) or groups() match object functions to retrieve matching expressions.
| Match Object Method | Description |
| group(num=0) | The string of the entire matched expression. group() can accept multiple group numbers at once, in which case it returns a tuple containing the corresponding values. |
| groups() | Returns a tuple containing all subgroup strings, from 1 to the number of subgroups. |
Example
Section titled “Example”The output of the above example is:
Example
Section titled “Example”The output of the above example is:
re.search Method
Section titled “re.search Method”re.search scans the entire string and returns the first successful match.
Function syntax:
Function parameter description:
| Parameter | Description |
| pattern | The regular expression pattern to match |
| string | The string to match. |
| flags | Flags to control the matching behavior of the regular expression, such as case sensitivity, multiline matching, etc. See: Regular Expression Modifiers - Optional Flags |
On successful match, re.search returns a match object; otherwise it returns None.
We can use the group(num) or groups() match object functions to retrieve matching expressions.
| Match Object Method | Description |
| group(num=0) | The string of the entire matched expression. group() can accept multiple group numbers at once, in which case it returns a tuple containing the corresponding values. |
| groups() | Returns a tuple containing all subgroup strings, from 1 to the number of subgroups. |
Example
Section titled “Example”The output of the above example is:
Example
Section titled “Example”The output of the above example is:
Difference Between re.match and re.search
Section titled “Difference Between re.match and re.search”re.match only matches at the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None. re.search, on the other hand, matches the entire string until a match is found.
Example
Section titled “Example”The output of the above example is:
Search and Replace
Section titled “Search and Replace”Python’s re module provides re.sub for replacing matches in strings.
Syntax:
Parameters:
- pattern : The pattern string in the regular expression.
- repl : The replacement string, which can also be a function.
- string : The original string to be searched and replaced.
- count : The maximum number of replacements after pattern matching. Default 0 replaces all matches.
- flags : The matching mode used during compilation, in numeric form.
The first three are required parameters; the last two are optional.
Example
Section titled “Example”The output of the above example is:
repl Parameter as a Function
Section titled “repl Parameter as a Function”In the following example, matched numbers in the string are multiplied by 2:
Example
Section titled “Example”Output:
compile Function
Section titled “compile Function”The compile function is used to compile a regular expression, generating a regular expression (Pattern) object for use by the match() and search() functions.
Syntax:
Parameters:
-
pattern : A regular expression in string form
-
flags : Optional, specifies the matching mode, such as case insensitivity, multiline mode, etc. Specific parameters are:
-
re.L Indicates that special character sets \w, \W, \b, \B, \s, \S depend on the current locale
-
re.MULTILINE or re.M - Multiline mode, changes the behavior of ^ and $ so they match the beginning and end of each line in the string.
-
re.DOTALL or re.S - Makes . match any character including newlines.
-
re.ASCII - Makes \w, \W, \b, \B, \d, \D, \s, \S only match ASCII characters.
-
re.VERBOSE or re.X - Ignores whitespace and comments, allowing for clearer organization of complex regular expressions.
These flags can be used individually or combined using bitwise OR (|). For example, re.IGNORECASE | re.MULTILINE enables both case insensitivity and multiline mode.
Example
Section titled “Example”Example
Section titled “Example”Above, when a match is successful, a Match object is returned, where:
group([group1, …])method is used to obtain one or more grouped match strings. To get the entire matched substring, you can directly usegroup()orgroup(0);start([group])method is used to get the starting position of the grouped match substring in the entire string (the index of the first character of the substring), default parameter value is 0;end([group])method is used to get the ending position of the grouped match substring in the entire string (the index of the last character + 1), default parameter value is 0;span([group])method returns(start(group), end(group)).
Let’s look at another example:
Example
Section titled “Example”findall
Section titled “findall”Finds all substrings in a string that match the regular expression and returns a list. If there are multiple matching patterns, returns a list of tuples. If no matches are found, returns an empty list.
Note: match and search match once; findall matches all.
Syntax:
Parameters:
- pattern The matching pattern.
- string The string to match.
- pos Optional parameter, specifies the starting position of the string, default is 0.
- endpos Optional parameter, specifies the ending position of the string, default is the length of the string.
Find all numbers in a string:
Example
Section titled “Example”Output:
Multiple matching patterns, returns a list of tuples:
Example
Section titled “Example”re.finditer
Section titled “re.finditer”Similar to findall, it finds all substrings matching the regular expression in a string and returns them as an iterator.
Parameters:
| Parameter | Description |
| pattern | The regular expression pattern to match |
| string | The string to match. |
| flags | Flags to control the matching behavior of the regular expression, such as case sensitivity, multiline matching, etc. See: Regular Expression Modifiers - Optional Flags |
Example
Section titled “Example”Output:
re.split
Section titled “re.split”The split method splits a string according to matching substrings and returns a list. Its usage is as follows:
Parameters:
| Parameter | Description |
| pattern | The regular expression pattern to match |
| string | The string to match. |
| maxsplit | Number of splits; maxsplit=1 splits once. Default is 0, no limit on number of splits. |
| flags | Flags to control the matching behavior of the regular expression, such as case sensitivity, multiline matching, etc. See: Regular Expression Modifiers - Optional Flags |
Example
Section titled “Example”Regular Expression Objects
Section titled “Regular Expression Objects”re.RegexObject
Section titled “re.RegexObject”re.compile() returns a RegexObject object.
re.MatchObject
Section titled “re.MatchObject”group() returns the string matched by the RE.
- start() returns the starting position of the match
- end() returns the ending position of the match
- span() returns a tuple containing the (start, end) positions of the match
Regular Expression Modifiers - Optional Flags
Section titled “Regular Expression Modifiers - Optional Flags”Regular expressions can include optional flag modifiers to control matching modes.
The following flags can be used individually or combined using bitwise OR (|). For example, re.IGNORECASE | re.MULTILINE enables both case insensitivity and multiline mode.
| Modifier | Description | Example |
|---|---|---|
| re.IGNORECASE or re.I | Makes matching case-insensitive | |
| re.MULTILINE or re.M | Multiline matching, affects ^ and $, making them match the beginning and end of each line in the string. | |
| re.DOTALL or re.S: | Makes . match any character including newlines. | |
| re.ASCII | Makes \w, \W, \b, \B, \d, \D, \s, \S only match ASCII characters. | |
| re.VERBOSE or re.X | Ignores whitespace and comments, allowing for clearer organization of complex regular expressions. | |
Regular Expression Patterns
Section titled “Regular Expression Patterns”Pattern strings use special syntax to represent a regular expression.
Letters and numbers represent themselves. Letters and numbers in a regular expression pattern match the same strings.
Most letters and numbers have different meanings when preceded by a backslash.
Punctuation marks only match themselves when escaped; otherwise they represent special meanings.
The backslash itself needs to be escaped with a backslash.
Since regular expressions often contain backslashes, it’s best to use raw strings to represent them. Pattern elements (like r’\t’, equivalent to \t) match the corresponding special characters.
The following table lists special elements in regular expression pattern syntax. If you provide optional flag parameters along with the pattern, the meaning of some pattern elements may change.
| Pattern | Description |
| ^ | Matches the beginning of a string |
| $ | Matches the end of a string. |
| . | Matches any character except newline. When the re.DOTALL flag is specified, it can match any character including newlines. |
| […] | Used to match any single character within the brackets, e.g. [amk] matches ‘a’, ‘m’, or ‘k’ |
| [^…] | Characters not in []: [^abc] matches characters other than a, b, c. |
| re* | Matches 0 or more occurrences of the expression. |
| re+ | Matches 1 or more occurrences of the expression. |
| re? | Matches 0 or 1 occurrence of the fragment defined by the preceding regular expression, non-greedy mode |
| re{ n} | Matches exactly n occurrences of the preceding expression. For example, “o{2}” cannot match “o” in “Bob”, but can match both o’s in “food”. |
| re{ n,} | Matches n or more occurrences of the preceding expression. For example, “o{2,}” cannot match “o” in “Bob”, but can match all o’s in “foooood”. “o{1,}” is equivalent to “o+”. “o{0,}” is equivalent to “o*”. |
| re{ n, m} | Matches n to m occurrences of the fragment defined by the preceding regular expression, greedy mode |
| a| b | Matches a or b |
| (re) | Matches the expression inside the parentheses and also represents a group |
| (?imx) | Regular expression includes three optional flags: i, m, or x. Only affects the area within the parentheses. |
| (?-imx) | Regular expression turns off i, m, or x optional flags. Only affects the area within the parentheses. |
| (?: re) | Similar to (…), but does not represent a group |
| (?imx: re) | Use i, m, or x optional flags within parentheses |
| (?-imx: re) | Do not use i, m, or x optional flags within parentheses |
| (?#…) | Comment. |
| (?= re) | Forward positive lookahead. Succeeds if the contained regular expression, represented by …, matches at the current position; otherwise fails. However, once the contained expression has been tried, the matching engine does not advance; the remaining part of the pattern still tries to match to the right of the lookahead. |
| (?! re) | Forward negative lookahead. Opposite of positive lookahead; succeeds when the contained expression cannot match at the current position in the string. |
| (?> re) | Independent pattern for matching, eliminating backtracking. |
| \w | Matches digits, letters, and underscore |
| \W | Matches non-digits, non-letters, and non-underscores |
| \s | Matches any whitespace character, equivalent to [\t\n\r\f]. |
| \S | Matches any non-whitespace character |
| \d | Matches any digit, equivalent to [0-9]. |
| \D | Matches any non-digit |
| \A | Matches the beginning of a string |
| \Z | Matches the end of a string. If a newline exists, only matches before the newline at the end of the string. |
| \z | Matches the end of a string |
| \G | Matches the position where the last match finished. |
| \b | Matches a word boundary, i.e. the position between a word and a space. For example, ‘er\b’ can match ‘er’ in “never”, but cannot match ‘er’ in “verb”. |
| \B | Matches a non-word boundary. ‘er\B’ can match ‘er’ in “verb”, but cannot match ‘er’ in “never”. |
| \n, \t, etc. | Matches a newline. Matches a tab, etc. |
| \1…\9 | Matches the content of the nth group. |
| \10 | Matches the content of the nth group, if it has been matched. Otherwise refers to an octal character code expression. |
Regular Expression Examples
Section titled “Regular Expression Examples”Character Matching
Section titled “Character Matching”| Example | Description |
| python | Matches “python”. |
Character Classes
Section titled “Character Classes”| Example | Description |
| [Pp]ython | Matches “Python” or “python” |
| rub[ye] | Matches “ruby” or “rube” |
| [aeiou] | Matches any single letter within the brackets |
| [0-9] | Matches any digit. Similar to [0123456789] |
| [a-z] | Matches any lowercase letter |
| [A-Z] | Matches any uppercase letter |
| [a-zA-Z0-9] | Matches any letter or digit |
| [^aeiou] | All characters except aeiou letters |
| [^0-9] | Matches characters other than digits |
Special Character Classes
Section titled “Special Character Classes”| Example | Description |
| . | Matches any single character except “\n”. To match any character including ‘\n’, use a pattern like ‘[.\n]’. |
| \d | Matches a digit character. Equivalent to [0-9]. |
| \D | Matches a non-digit character. Equivalent to [^0-9]. |
| \s | Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v]. |
| \S | Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v]. |
| \w | Matches any word character including underscore. Equivalent to ‘[A-Za-z0-9_]’. |
| \W | Matches any non-word character. Equivalent to ‘[^A-Za-z0-9_]’. |