Python - Regex split() Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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() methodre.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() functionSplitting a String by Comma in Pythonre.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 SplitsWe 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 GroupsThis 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'] Comment More infoAdvertise with us Next Article Python - Regex split() V vishakshx339 Follow Improve Article Tags : Python python-regex python Practice Tags : pythonpython 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 re.search() in Python re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example:Pythonimport re s = "Hello, welcome to the worl 3 min read Python String rsplit() Method Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator. It's similar to the split() method in Python, but the difference is that rsplit() starts splitting from the end of the string rather than from the beginning. Exampl 3 min read Python Regex: Replace Captured Groups Regular Expressions, often abbreviated as Regex, are sequences of characters that form search patterns. They are powerful tools used in programming and text processing to search, match, and manipulate strings. Think of them as advanced search filters that allow us to find specific patterns within a 5 min read Python splitfields() Method The splitfields() method is a user-defined method written in Python that splits any kind of data into a list of fields using a delimiter. The delimiter can be specified as an argument to the method, and if no delimiter is specified, the method splits the string using whitespace characters as the del 3 min read Pattern matching in Python with Regex You may be familiar with searching for text by pressing ctrl-F and typing in the words youâre looking for. Regular expressions go one step further: They allow you to specify a pattern of text to search for. In this article, we will see how pattern matching in Python works with Regex.Regex in PythonR 8 min read Like