Reference · Regular expressions
re.split
re.split(str, str, str | null=) -> listWhat it does
The text cut at every match of a pattern, as a list of the pieces between the
matches. The matches themselves are left out. Where the separator is plain text,
split does the same without a pattern. The flags come
last: "i", "m", "s".
Examples
print(re.split("a, b,c , d", "\\s*,\\s*"))print(re.split("a1b22c", "\\d+"))print(re.split("one\ntwo\r\nthree", "\\r?\\n"))print(re.split("k1=v1;k2=v2", "[;=]"))["a", "b", "c", "d"]["a", "b", "c"]["one", "two", "three"]["k1", "v1", "k2", "v2"]Edge cases
- Empty pieces are kept: a match at the start, at the end or twice in a row gives
""there. - A pattern that matches nothing gives the whole text as the only piece; the empty
text gives
[""]. - Groups in the pattern are not put into the list, unlike JavaScript's
split. - The empty pattern cuts between every two characters, with
""at both ends; a pattern that can match the empty text (a*) gives empty pieces where it does. - Characters, not bytes:
"añb"cut atñis["a", "b"].
print(re.split(",a,,b,", ","), re.split("x", "x"))print(re.split("abc", "x"), re.split("", "x"))print(re.split("a1b", "(\\d)"), re.split("A-b_C", "[-_]", "i"))print(re.split("ab", ""), re.split("aaa", "a*"))print(re.split("añb", "ñ"))["", "a", "", "b", ""] ["", ""]["abc"] [""]["a", "b"] ["A", "b", "C"]["", "a", "b", ""] ["", "", ""]["a", "b"]Errors
A pattern that is not supported (lookaround, backreferences, \p{...}, a bad escape,
a bad flag) is a runtime error, not an error value, as is a text that is not a
string. See re.test for what a pattern may hold.
print(re.split("abc", "(?!b)"))line 1: `re.split`: lookahead and lookbehind ((?= (?! (?<= (?<!) are not supportedprint(re.split(42, ","))line 1: `re.split` expects a str as argument 1, got intChaining
The text comes first in a pipeline, and the pieces go on to map or filter:
print("a b c" | re.split("\\s+") | map(upper))print("one, two,,three" | re.split("\\s*,\\s*") | filter((p) => p != ""))let [key, value] = re.split("name = Ann", "\\s*=\\s*")print(key, value)["A", "B", "C"]["one", "two", "three"]name AnnSee also
split, re.find_all, re.replace, join, filter
Thread 0 posts
On re.split(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo