How to Split a String Using Regex in Python

In this Python programming tutorial, we will learn how to split a string using regular expressions (regex) in Python. We will be using the re module, which is part of the Python standard library. Let’s take a few examples and see how we can split a string using regex in Python.

Split a String Using Regex in Python

Below are the steps we will follow to split a string using regex in Python.

  1. Import the re module
  2. Define the input string
  3. Define the regex pattern
  4. Use re.split() function to split the string
  5. Print the output

Step-1: Import the re module

First, we need to import the re module to use the regex functions:

import re

Step-2: Define the input string

In this example, we will use the string “United States of America”:

input_string = "United States of America"

Step-3: Define the regex pattern

We need to define the regex pattern that we will use to split the input string in Python. Let’s say we want to split the string at each space character. We can use the regex pattern \s to match any whitespace character:

pattern = r'\s'

Step-4: Use re.split() function to split the string

Now, we will use the Python re.split() function from the re module to split the input string based on the defined regex pattern:

split_string = re.split(pattern, input_string)

Step-5: Print the output

Finally, we can print the output to see the result:

print(split_string)

This will give the output:

['United', 'States', 'of', 'America']

Here is the complete code to Split a String Using Regex in Python.

import re

# Step 2: Define the input string
input_string = "United States of America"

# Step 3: Define the regex pattern
pattern = r'\s'

# Step 4: Use re.split() function to split the string
split_string = re.split(pattern, input_string)

# Step 5: Print the output
print(split_string)
How to Split a String Using Regex in Python
How to Split a String Using Regex in Python

This is how to split a string in Python using regex.

READ:  Find first number in string in Python

Let us see a few more examples of how to split a string in Python using regex with different regex patterns and input strings.

Example-1: Splitting a string with multiple whitespace characters

Input string: "United States\t of \nAmerica"

Pattern: \s+ (matches one or more whitespace characters)

import re

input_string = "United    States\t  of \nAmerica"
pattern = r'\s+'

split_string = re.split(pattern, input_string)
print(split_string)

Output:

['United', 'States', 'of', 'America']

Example-2: Splitting a string on commas and spaces

Input string: "apple, banana,grape, cherry"

Pattern: \s*,\s* (matches a comma with zero or more whitespace characters before and after)

import re

input_string = "apple, banana,grape, cherry"
pattern = r'\s*,\s*'

split_string = re.split(pattern, input_string)
print(split_string)

Output:

['apple', 'banana', 'grape', 'cherry']

Example-3: Splitting a string with a mixture of delimiters

Input string: "apple; banana, grape|cherry"

Pattern: \s*[,;|]\s* (matches a comma, semicolon, or vertical bar with zero or more whitespace characters before and after)

import re

input_string = "apple; banana, grape|cherry"
pattern = r'\s*[,;|]\s*'

split_string = re.split(pattern, input_string)
print(split_string)

Output:

['apple', 'banana', 'grape', 'cherry']

Example-4: Splitting a string with digits as delimiters

Input string: "apple1banana22grape333cherry"

Pattern: \d+ (matches one or more digits)

import re

input_string = "apple1banana22grape333cherry"
pattern = r'\d+'

split_string = re.split(pattern, input_string)
print(split_string)

Output:

['apple', 'banana', 'grape', 'cherry']

Conclusion

In this tutorial, we learned how to split a string using regular expressions (regex) in Python. By leveraging the powerful re module, we can effectively split strings with complex patterns and varying delimiters. The re.split() function allows us to define and use custom regex patterns to match specific requirements and accurately split input strings.

We covered different examples to demonstrate the flexibility and versatility of the re.split() function, including cases with multiple whitespace characters, various delimiters, and mixed delimiters.

You may also like the following Python string tutorials: