Split strings on multiple patterns


In [1]:
'hello you'.split()


Out[1]:
['hello', 'you']

In [3]:
'hello | you'.split('|')


Out[3]:
['hello ', ' you']

In [9]:
# Doesn't seem to like more than one delimiter
'hello | you; and, me'.split(',|;')


Out[9]:
['hello | you; and, me']

In [10]:
import re

In [13]:
# Pipe-separated delimiters
# Both "," and ";" are split on
re.split(',|;', 'hello | you; and, me')


Out[13]:
['hello | you', ' and', ' me']

In [14]:
# What if you want to split on "|" too?
# Escape it: \|
re.split(',|;|\|', 'hello | you; and, me')


Out[14]:
['hello ', ' you', ' and', ' me']