re.split() in Python (original) (raw)

Last Updated : 30 Dec, 2024

The **re.split() method in Python is used to split a string by a pattern (using regular expressions). It is part of the re-module, which provides support for working with regular expressions. This method is helpful when we need to split a string into multiple parts based on complex patterns rather than just simple delimiters like spaces or commas.

Let’s understand how to use re.split() with an example.

Python `

import re

Original string

s = "geeks,for;geeks gfg"

Split the string by commas, semicolons, or spaces

result = re.split(r"[,\s;]+", s)

print(result)

`

Output

['geeks', 'for', 'geeks', 'gfg']

Syntax of re.split()

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

Parameters

Return Type

Limiting the Number of Splits

In some cases, we may not want to split the string into as many parts as possible. Instead, we might want to limit the number of splits, so that the result has fewer elements. This is where the maxsplit argument comes in. It controls how many times the string will be split.

Python `

import re

Original string

s = "geeks for geeks gfg"

Split the string by space, but limit the number of splits to 2

result = re.split(r"\s+", s, maxsplit=2)

print(result)

`

Output

['geeks', 'for', 'geeks gfg']

**Explanation

Splitting with Capture Groups

One powerful feature of regular expressions is the ability to use capture groups. A **capture group is a part of the regular expression pattern enclosed in parentheses. When we use capture groups in re.split(), the matched part of the pattern is included in the result along with the rest of the string.

Python `

import re

Original string

s = "geeks123for456geeks"

Split the string at numbers, keeping the numbers in the result

result = re.split(r"(\d+)", s)

print(result)

`

Output

['geeks', '123', 'for', '456', 'geeks']

**Explanation