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.
Similar Reads
Python | Custom list split Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed. This is quite a useful utility to have knowledge about. Let's discuss certain ways in which this task can be performed. Method
8 min read
How to Split Lists in Python? Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
3 min read
Python - Selectively Split in Strings Sometimes, while working with Python strings, we may have to perform a split. Not sometimes, normal one, depending on deliminator but something depending upon programming constructs like elements, numbers, words etc and segregate them. Lets discuss a way in which this task can be solved. Method : Us
3 min read
Python - Reversed Split Strings In Python, there are times where we need to split a given string into individual words and reverse the order of these words while preserving the order of characters within each word. For example, given the input string "learn python with gfg", the desired output would be "gfg with python learn". Let
3 min read
Python - Split in Nested tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform split of elements in nested tuple, by a certain delimiter. This kind of problem can have application in different data domains. Let's discuss certain ways in which this task can be performed. Input : test_
7 min read