re.split

str.split for patterns. Capturing groups in the separator are kept in the result, and a separator at either end produces an empty string there. Also available as Pattern.split(string, maxsplit=0).

re functionPython 3 (all)Live demo
Common call
re.split(r'[,;]\s*', 'a, b;c')
Returns
['a', 'b', 'c']
Replaces
several str.split / str.replace passes for mixed separators
Watch out
Separators at the start or end give empty strings; groups insert extra items
re.split(patternpattern — The separator, as a raw-string regular expression.type: str | re.Pattern · required, stringstring — The text to split.type: str · required, maxsplitmaxsplit — At most this many splits; the rest stays in the last item. 0 = no limit. Pass by keyword (positional is deprecated since 3.13).type: int · default: 0=0, flagsflags — Flags such as re.IGNORECASE. Pass by keyword.type: re.RegexFlag | int · default: 0=0)
→ list[str | None]

Demo

Live evaluation
Text between the matches, in order.
Try:
Inputs
patternstrthe separator pattern
textstrtext to split
Code
import re
re.split('\\s*[,;|]\\s*', 'red , green;blue| alpha')
Result
['red', 'green', 'blue', 'alpha']

Leading or trailing separators produce empty strings — ',a,,b,' splits into five items, three of them empty. With a group, ([+-]), each operator appears between the numbers. The empty pattern matches between every character, so it splits 'abc' into single letters with an empty string at each end (allowed since Python 3.7).

Parameters

NameTypeRequiredDescription
patternstr | re.PatternyesThe separator, as a raw-string regular expression.
stringstryesThe text to split.
maxsplitintno (0)At most this many splits; the rest stays in the last item. 0 = no limit. Pass by keyword (positional is deprecated since 3.13).
flagsre.RegexFlag | intno (0)Flags such as re.IGNORECASE. Pass by keyword.

Return value

list[str | None] — The pieces between matches; with capturing groups, the group texts are inserted between them (None for groups that did not match).

Common patterns

Split on several separators
A character class of separators, eating the surrounding spaces.
import re
parts = re.split(r'\s*[,;]\s*', line.strip())
Tokenize and keep operators
Capture the separator, drop empty pieces.
import re
tokens = [t for t in re.split(r'\s*([-+*/()])\s*', expr) if t]
Split into lines, any line ending
str.splitlines already does this — prefer it; the regex shows the idea.
import re
lines = re.split(r'\r\n|\r|\n', text)

Examples

1. Several separators
import re re.split(r'[,;]\s*', 'a, b;c, d')
Returns
['a', 'b', 'c', 'd']
2. A group keeps the separators
import re re.split(r'([,;])\s*', 'a, b;c')
Returns
['a', ',', 'b', ';', 'c']
3. maxsplit
import re re.split(r',', 'a,b,c,d', maxsplit=2)
Returns
['a', 'b', 'c,d']
4. Empty strings at the ends
import re re.split(r',', ',a,')
Returns
['', 'a', '']
5. Unmatched group gives None
import re re.split(r'(,)|;', 'a,b;c')
Returns
['a', ',', 'b', None, 'c']
6. Empty matches split too
import re re.split(r'x*', 'axb')
Returns
['', 'a', '', 'b', '']
7. Compiled: Pattern.split
import re re.compile(r'\d+').split('a1b22c')
Returns
['a', 'b', 'c']

Pitfalls

1. Splitting padded text on whitespace
Leading/trailing whitespace becomes empty strings. For plain whitespace splitting, str.split() with no argument is simpler.
re.split(r'\s+')
import re
re.split(r'\s+', '  a b  ')
['', 'a', 'b', '']
str.split()
'  a b  '.split()
['a', 'b']
2. A group you did not mean to keep
Parentheses used only for alternation also capture, so the separators show up in the result. Use (?:...).
(and|or)
import re
re.split(r'\s(and|or)\s', 'tea and cake or pie')
['tea', 'and', 'cake', 'or', 'pie']
(?:and|or)
import re
re.split(r'\s(?:and|or)\s', 'tea and cake or pie')
['tea', 'cake', 'pie']
3. maxsplit passed positionally
Python 3.13 deprecates it (a DeprecationWarning), and it is easy to confuse with flags. Name it.
re.I as 3rd arg
import re
re.split(r'x', 'aXbxc', re.I)
['aXb', 'c']
flags=re.I
import re
re.split(r'x', 'aXbxc', flags=re.I)
['a', 'b', 'c']

When to use

Use it
  • Separators that vary (several characters, optional spaces, words)
  • Tokenizing while keeping the delimiters (capturing group)
Reach for something else
  • One fixed separator → str.split(sep)
  • Whitespace → str.split() with no argument
  • Lines → str.splitlines()
  • CSV with quoted fields → the csv module

Notes

CPython impl
pattern_split in Modules/_sre/_sre.c: after each match the text before it is appended, then every group (None if it did not participate)
Empty matches
Splitting on a pattern that can match the empty string is allowed since Python 3.7
maxsplit
Positional maxsplit (and flags) is deprecated since 3.13; negative maxsplit means no splits at all

FAQ

re.split(r'[,;|]', text) — a character class lists single-character separators; use alternation for longer ones: re.split(r'\s*(?:and|or)\s*', text).