Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

re.split

Splits input at regular-expression matches.

Version: Python 3.14; repository image, production runtime not attested

Syntax

re.split(pattern, string, maxsplit=0, flags=0)

Splits input at regular-expression matches.

Capturing parentheses keep matched separators in the resulting list. A trailing separator creates an empty final field.

Split wherever the pattern matches and return the intervening fields. Capturing parentheses insert the captured delimiter text into the list; a trailing delimiter leaves an empty field. maxsplit=0 means unlimited, while maxsplit=1 retains the rest intact after the first split. Pass maxsplit and flags by name in 3.14. An empty separator splits ab into four fields including the empty strings at both ends. Positional maxsplit and flags have been deprecated since 3.13 and still work with DeprecationWarning in 3.14.

import re
assert re.split(r"([,;])", "a,b;") == ["a", ",", "b", ";", ""]
assert re.split(r"[,;]", "a,b;c", maxsplit=1) == ["a", "b;c"]
assert re.split(":", "abc") == ["abc"]
assert re.split("", "ab") == ["", "a", "b", ""]

Examples

import re
print(re.split(r"([,;])", "a,b;"))
print(re.split("", "ab"))

Result

['a', ',', 'b', ';', '']
['', 'a', 'b', '']

Split wherever the pattern matches and return the intervening fields. Capturing parentheses insert the captured delimiter text into the list; a trailing delimiter leaves an empty field. maxsplit=0 means unlimited, while maxsplit=1 retains the rest intact after the first split. Pass maxsplit and flags by name in 3.14. An empty separator splits ab into four fields including the empty strings at both ends. Positional maxsplit and flags have been deprecated since 3.13 and still work with DeprecationWarning in 3.14.

Sources