re.split()
Published Jul 30, 2021Updated Jun 10, 2022
Contribute to Docs
The .split()
method of the re
module divides a string into substrings at each occurrence of the specified character(s). This method is a good alternative to the default .split()
string method for instances that require matching multiple characters.
Syntax
re.split(<pattern>, string, <maxsplit>, <flags>)
A <pattern>
is a regular expression that can include any of the following:
- A string:
Jane Smith
- A character class code:
/w
,/s
,/d
- A regex symbol:
$
,|
,^
The other arguments include:
- An integer for the maximum number of splits (max split):
4
- Flags:
IGNORECASE
,VERBOSE
,DOTALL
Example
The following example splits the text at each parenthesis:
import rerace_info = '''The top three race finishers were: Jane Smith (2:14), Sarah Long (2:18) and Suzy Reynolds (2:20).'''re.split(r"\(|\)", race_info)# A backslash indicates that the parentheses are part of the pattern
All contributors
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.