Regular Expressions

Split string with multiple delimiters in Python

六二三
Python string split() method allows a string to be easily split into a list based on a delimiter. Though in some cases, you might need the separation to occur based on not just one but multiple delimiter values. This quick 101 article introduces two convenient approaches this can be achieved in Python. Split String With Two Delimiters in Python Assume the following string. text = "python is, an easy;language; to, learn." For our example, we need to split it either by a semicolon followed by a space ;, or by a comma followed by a space ,. In this case, any occurrences of singular semicolons or commas i.e. , , ; with no trailing spaces should not be concerned. Regular Expressions A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values. Use Basic Expression Python’s built-in module re has a split() method we can use for this case. Let’s use a basic a or b regular expression (a|b) for separating our multiple delimiters. import re text = "python is, an easy;language; to, learn." print(re.split('; |, ', text)) Output: ['python is', 'an easy;language', 'to', 'learn.'] As mentioned on the Wikipedia page, Regular Expressions use IEEE POSIX as the standard for its syntax.