Open In App

re.split() in Python

Last Updated : 30 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • pattern: The regular expression (pattern) that we want to use to split the string.
  • string: The original string to be split.
  • maxsplit (optional): The maximum number of splits to do. If omitted or set to 0, there is no limit to the number of splits.
  • flags (optional): Flags to modify the behavior of the regular expression, such as re.IGNORECASE for case-insensitive matching.

Return Type

  • The method returns a list of strings, which are the parts of the original string split by the specified pattern.

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

  • The string is split into a maximum of 3 parts (because maxsplit=2 limits the splits to 2). The output will contain the first 2 splits and the rest will stay in the last part.

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

  • The pattern (\d+) matches one or more digits. The parentheses create a capture group, so the numbers are included in the result along with the other parts of the string.

Next Article
Practice Tags :

Similar Reads