Hướng dẫn python array delimiter
Here's a safe way for any iterable of delimiters, using regular expressions: Show
re.escape allows to build the pattern automatically and have the delimiters escaped nicely. Here's this solution as a function for your copy-pasting pleasure:
If you're going to split often using the same delimiters, compile your regular expression beforehand like described and use If you'd like to leave the original delimiters in the string, you can change the regex to use a lookbehind assertion instead:
(replace In this python tutorial, you will learn how to split a string with multiple delimiters. Table Of Contents
Split a string with a single delimiter using split()Here, we will split a string into multiple substring based on a single delimeter. The split() function is available in the re module. It splits a string into substrings and returns a list of strings separated by a delimiter. Syntax: re.split('delimiter',inp_str) It takes two parameters. Advertisements
It splits the string into multiple substrings based on the delimeter and returns that list. Example 1: In this example, we will have a string that has a comma(,) delimiter – “Welcome, to this Pointer, Python”. Now we will split this string based on comma. import re # Consider the string inp_str= "Welcome, to this Pointer, Python" print("Actual String: ",inp_str) # Split the string with single delimiter. listOfstrs = re.split(',',inp_str) print("After Splitting the string with single delimiter: ", listOfstrs) Output: Actual String: Welcome, to this Pointer, Python After Splitting the string with single delimiter: ['Welcome', ' to this Pointer', ' Python'] We can see that string is split into three strings. Example 2: In this example, we have a string that has a hyphen(-) delimiter – “Welcome- to this Pointer- Python”. Now we will split this string. import re # Consider the string inp_str= "Welcome- to this Pointer- Python" print("Actual String: ",inp_str) # Split the string with single delimiter. listOfstrs = re.split('-',inp_str) print("After Splitting the string with single delimiter: ", listOfstrs) Output: Actual String: Welcome- to this Pointer- Python After Splitting the string with single delimiter: ['Welcome', ' to this Pointer', ' Python'] We can see that string is split into three strings. Here, we will split a string based on different delimiters and get all substrings in a list of strings. The split() function is available in the re module, so first we have to import that. To specify multiple delimiters, we have to separate each delimiter with ‘|’ while passing them into split() function. Syntax: re.split('demimiter1 | delimiter2 |.....',inp_str) It takes two parameters. It splits the string based on multiple delimeters provided in the first argument and returns a list of substrings. Example 1: In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer – Python”. Now we will split this string based on three delimters i.e. ‘,’ , ‘/’ and ‘-‘. import re inp_str= "Welcome, to this / Pointer - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(',|/|-', inp_str) print(listOfstrs) Output: Actual String: Welcome, to this / Pointer - Python ['Welcome', ' to this ', ' Pointer ', ' Python'] We can see that string is split into four strings. Example 2: In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer,c^programming & – Python”. Now we will split this string with multiple delimeters. import re inp_str= "Welcome, to this / Pointer,c^programming & - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(',|/|-|^|&', inp_str) print(listOfstrs) Output: Actual String: Welcome, to this / Pointer,c^programming & - Python ['', 'Welcome', ' to this ', ' Pointer', 'c^programming ', ' ', ' Python'] We can see that string is split into seven strings. Split a string with multiple delimiters using split() with ‘[]’To split a string with different delimeters, specify them inside [] separated by a comma and pass as argument to regex’s split() function. Syntax: re.split(r'[delimiter1,delimiter2,............]\s*',inp_str) It takes two parameters. It splits the given string into multiple substrings based on the specified delimeters and returns those substrings in a list. Example 1: In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer – Python”. Now we will split this string using different delimters. import re inp_str= "Welcome, to this / Pointer - Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(r'[,,/,-]\s*', inp_str) print(listOfstrs) Output: Actual String: Welcome, to this / Pointer - Python ['Welcome', 'to this ', 'Pointer ', 'Python'] We can see that string is split into four strings. Example 2: import re inp_str= "Welcome, to this / Pointer,c^programming &- Python" print("Actual String: ",inp_str) # Split the string with multiple delimiters listOfstrs = re.split(r'[,/,-,^,&]\s*',inp_str) print(listOfstrs) Output: Actual String: Welcome, to this / Pointer,c^programming &- Python ['Welcome', 'to this ', 'Pointer', 'c', 'programming ', '- Python'] We can see that string is split into six strings. SummaryIn this article, we learned how to split strings with single and multiple delimiters using the split() method. Happy Coding. |