Hướng dẫn python array delimiter

Here's a safe way for any iterable of delimiters, using regular expressions:

>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "stackoverflow (c) is awesome... isn't it?"
>>> regex_pattern = '|'.join(map(re.escape, delimiters))
>>> regex_pattern
'a|\\.\\.\\.|\\(c\\)'
>>> re.split(regex_pattern, example)
['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]

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:

def split(delimiters, string, maxsplit=0):
    import re
    regex_pattern = '|'.join(map(re.escape, delimiters))
    return re.split(regex_pattern, string, maxsplit)

If you're going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split.


If you'd like to leave the original delimiters in the string, you can change the regex to use a lookbehind assertion instead:

>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "stackoverflow (c) is awesome... isn't it?"
>>> regex_pattern = '|'.join('(?<={})'.format(re.escape(delim)) for delim in delimiters)
>>> regex_pattern
'(?<=a)|(?<=\\.\\.\\.)|(?<=\\(c\\))'
>>> re.split(regex_pattern, example)
['sta', 'ckoverflow (c)', ' is a', 'wesome...', " isn't it?"]

(replace ?<= with ?= to attach the delimiters to the righthand side, instead of left)

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()
  • Split a string with multiple delimiters using split() with ‘|’
  • Split a string with multiple delimiters using split() with ‘[]’
  • Summary

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

  1. the first parameter is the delimiter
  2. the second parameter is the input string.

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.
1. the first parameter takes multiple delimiters separated by |.
2. the second parameter is the input string.

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.
1. the first parameter takes multiple delimiters separated by ‘,’ inside []
2. the second parameter is the input string.

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:
In this example, we have a string that has multiple delimiters – “Welcome, to this / Pointer,c^programming &- Python”. Now we will split this string.

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.

Summary

In this article, we learned how to split strings with single and multiple delimiters using the split() method. Happy Coding.