re.split() method in Python is generally used to split a string by a specified pattern. Its working is similar to the standard split() function but adds more functionality. Let’s start with a simple example of re.split() method:
Python
import re
s = "Geeks,for,Geeks"
# Using re.split() to split the string
result = re.split(r',', s)
print(result)
Output['Geeks', 'for', 'Geeks']
Syntax of re.split() method
re.split(pattern, string, maxsplit=0, flags=0)
Parameters:
- pattern (required): This regular expression pattern that defines where the string should be split.
- string (required): This input string to be split based on the given pattern.
- maxsplit (optional, default is 0): This maximum number of splits to perform;
0
means no limit.
Return Type: This re.split() method returns a list of strings.
Examples of python regex split() function
Splitting a String by Comma in Python
re.split() function from the re module is used when you need more flexibility, such as handling multiple spaces or other delimiters.
Python
import re
s = "Geeks for Geeks"
# Regular expression to match one or more non-word characters
pattern = r'\W+'
# Split the string using the pattern
words = re.split(pattern, s)
print(words)
Output['Geeks', 'for', 'Geeks']
Explanation:
- This regex \W+ splits the string at non-word characters.
- Since there are none in the sentence, it returns a list of words.
Splitting Strings with a Maximum Number of Splits
We can use re.split() from the re module to split a string with a regular expression and specify a maximum number of splits using the maxsplit parameter.
Python
import re
s = "Geeks for Geeks"
#Using re.split() to split the string by spaces with a maximum of 2 splits
result = re.split(r' ', s, maxsplit=2)
print(result)
Output['Geeks', 'for', 'Geeks']
Explanation:
- This splits the string s at spaces, limiting the splits to a maximum of 2.
Splitting a String with Capturing Groups
This splits the string at the specified delimiters (comma and semicolon), but because of the capturing group, the delimiters themselves are retained in the output list.
Python
import re
s = "Geeks, for; Geeks"
# Using split() with a capturing group to keep the delimiters
result = re.split(r'(,|;)', s)
print(result)
Output['Geeks', ',', ' for', ';', ' Geeks']
Similar Reads
Python RegEx In this tutorial, you'll learn about RegEx and understand various regular expressions. Regular ExpressionsWhy Regular ExpressionsBasic Regular ExpressionsMore Regular ExpressionsCompiled Regular Expressions A RegEx is a powerful tool for matching text, based on a pre-defined pattern. It can detect t
9 min read
Regex Cheat Sheet - Python Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern.
9 min read
Verbose in Python Regex In this article, we will learn about VERBOSE flag of the re package and how to use it. re.VERBOSE : This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pat
3 min read
Python String split() Python String split() method splits a string into a list of strings after breaking the given string by the specified separator.Example:Pythonstring = "one,two,three" words = string.split(',') print(words) Output:['one', 'two', 'three']Python String split() Method SyntaxSyntax: str.split(separator, m
6 min read
How to Import Regex in Python Regex is a built-in library in Python. You can use the re module to work with regular expressions. Here are some basic examples of how to use the re module in Python:Examples of Regular Expression in PythonWe can import it in our Python by writing the following import statement in the Python script.
2 min read