![]() |
|
[Tut] Python | Split String with Regex - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String with Regex (/thread-100394.html) |
[Tut] Python | Split String with Regex - xSicKxBot - 12-13-2022 Python | Split String with Regex Rate this post Summary: The different methods to split a string using regex are:
[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']![]() 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'![]() 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![]() 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)![]() Code: Code: import re text = "abc_lmn_xyz"
pattern = re.compile("[^-\s]+").findall
print(pattern(text)) # ['abc', 'lmn', 'xyz']
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! ![]() 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. |