all Python 3refunction
re.split()
splitting
Splits a string at the matches of the pattern. Groups in the pattern are included in the result.
Syntax
re.split(pattern, string, maxsplit=0, flags=0)
maxsplit=0 means unlimited, not "no splits" — pass a positive number to cap it. And unlike str.split() with no arguments, re.split() tidies nothing up: a separator at either end leaves empty strings at the edges of the result, and since Python 3.7 a pattern that can match the empty string splits at every position instead of raising ValueError.
Examples
import re
re.split(r'\s+', 'one two three') # → ['one', 'two', 'three']
re.split(r'[,;]', 'a,b;c') # → ['a', 'b', 'c']
re.split(r'(\d+)', 'a1b2c') # → ['a', '1', 'b', '2', 'c']
re.split(r',', 'a,b,c', maxsplit=1) # → ['a', 'b,c']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy