12-13-2022, 06:04 AM
Python | Split String with Regex
Rate this post
Summary: The different methods to split a string using regex are:
[b]Minimal Example[/b]
Problem Formulation
Problem: Given a string and a delimiter. How will you split the string using the given delimiter using different functions from the regular expressions library?
Example: In the following example, the given string has to be split using a hyphen as the delimiter.
Method 1: re.split
The method matches all occurrences of the in the and divides the string along the matches resulting in a list of strings between the matches. For example, results in the list of strings .
![[Image: image-166.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-166.png)
Approach: Use the function and pass as the pattern which splits the given string on occurrence of an underscore.
Code:
Related Read: Python Regex Split
Method 2: re.sub
The regex function replaces all occurrences of the pattern with the replacement in string . It returns a new string. For example, if you call , the result will be the new string with all characters replaced by .
![[Image: image-167.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-167.png)
Approach: The idea here is to use the function to replace all occurrences of underscores with a space and then use the split function to split the string at spaces.
Code:
Related Read: Python Regex Sub
Method 3: re.findall
The method scans from left to right, searching for all non-overlapping matches of the . It returns a list of strings in the matching order when scanning the string from left to right.
![[Image: image-168.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-168.png)
Approach: Find all occurrences of characters that are separated by underscores using the.
Code:
Related Read: Python re.findall()
Method 4: re.compile
The method returns a regular expression object from the that provides basic regex methods such as , , and . The explicit two-step approach of (1) compiling and (2) searching the pattern is more efficient than calling, say, at once, if you match the same pattern multiple times because it avoids redundant compilations of the same pattern.
![[Image: image-170.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-170.png)
Code:
[b]Why use re.compile?[/b]
Read: Is It Worth Using Python’s re.compile()?
[b]Exercise[/b]
Problem: Python regex split by spaces, commas, and periods, but not in cases like 1,000 or 1.50.
Given:
Expected Output:
Solution
Conclusion
Therefore, we have learned four different ways of splitting a string using the regular expressions package in Python. Feel free to use the suitable technique that fits your needs. The idea of this tutorial was to get you acquainted with the numerous ways of using regex to split a string and I hope it helped you.
Please stay tuned and subscribe for more interesting discussions and tutorials in the future. Happy coding!![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Rate this post
Summary: The different methods to split a string using regex are:
- re.split()
- re.sub()
- re.findall()
- re.compile()
[b]Minimal Example[/b]
Code:
import re text = "Earth:Moon::Mars:Phobos" # Method 1
res = re.split("[:]+", text)
print(res) # Method 2
res = re.sub(r':', " ", text).split()
print(res) # Method 3
res = re.findall("[^:\s]+", text)
print(res) # Method 4
pattern = re.compile("[^:\s]+").findall
print(pattern(text)) # Output
['Earth', 'Moon', 'Mars', 'Phobos']Problem Formulation
Problem: Given a string and a delimiter. How will you split the string using the given delimiter using different functions from the regular expressions library?Example: In the following example, the given string has to be split using a hyphen as the delimiter.
Code:
# Input
text = "abc-lmn-xyz" # Expected Output
['abc', 'lmn', 'xyz']Method 1: re.split
The
Code:
re.split(pattern, string)Code:
patternCode:
stringCode:
re.split('a', 'bbabbbab')Code:
['bb', 'bbb', 'b']![[Image: image-166.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-166.png)
Approach: Use the
Code:
re.splitCode:
[_]+Code:
Code:
import re text = "abc_lmn_xyz"
res = re.split("[_]+", text)
print(res) # ['abc', 'lmn', 'xyz']
Related Read: Python Regex SplitMethod 2: re.sub
The regex function
Code:
re.sub(P, R, S)Code:
PCode:
RCode:
SCode:
re.sub('a', 'b', 'aabb')Code:
'bbbb'Code:
'a'Code:
'b'![[Image: image-167.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-167.png)
Approach: The idea here is to use the
Code:
re.subCode:
Code:
import re text = "abc_lmn_xyz"
res = re.sub(r'_', " ", text).split()
print(res) # ['abc', 'lmn', 'xyz']
Related Read: Python Regex SubMethod 3: re.findall
The
Code:
re.findall(pattern, string)Code:
stringCode:
pattern![[Image: image-168.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-168.png)
Approach: Find all occurrences of characters that are separated by underscores using the
Code:
re.findall()Code:
Code:
import re text = "abc_lmn_xyz"
res = re.findall("[^_\s]+", text)
print(res) # ['abc', 'lmn', 'xyz']
Related Read: Python re.findall()Method 4: re.compile
The method
Code:
re.compile(pattern)Code:
patternCode:
pattern.search(string)Code:
pattern.match(string)Code:
pattern.findall(string)Code:
search(pattern, string)![[Image: image-170.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-170.png)
Code:
Code:
import re text = "abc_lmn_xyz"
pattern = re.compile("[^-\s]+").findall
print(pattern(text)) # ['abc', 'lmn', 'xyz']- Efficiency: Using to assemble regular expressions is effective when the expression has to be used more than once. Thus, by using the classes/objects created by compile function, we can search for instances that we need within different strings without having to rewirte the expressions again and again. This increases productivity as well as saves time.Code:
re.compile()
- Readability: Another advantage of using is the readability factor as it leverages you the power to decouple the specification of the regex.Code:
re.compile
Read: Is It Worth Using Python’s re.compile()?[b]Exercise[/b]
Problem: Python regex split by spaces, commas, and periods, but not in cases like 1,000 or 1.50.
Given:
Code:
my_string = "one two 3.4 5,6 seven.eight nine,ten"Expected Output:
Code:
["one", "two", "3.4", "25.6" , "seven", "eight", "nine", "ten"]Solution
Code:
my_string = "one two 3.4 25.6 seven.eight nine,ten"
res = re.split('\s|(?<!\d)[,.](?!\d)', my_string)
print(res) # ['one', 'two', '3.4', '25.6', 'seven', 'eight', 'nine', 'ten']Therefore, we have learned four different ways of splitting a string using the regular expressions package in Python. Feel free to use the suitable technique that fits your needs. The idea of this tutorial was to get you acquainted with the numerous ways of using regex to split a string and I hope it helped you.
Please stay tuned and subscribe for more interesting discussions and tutorials in the future. Happy coding!
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

