Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python re.split on "," or " " [duplicate]

Tags:

python

regex

Trying to get re.split to work correctly. Input = "a1 a2 a3, a4,a5"

expecting output = ['a1','a2','a3','a4','a5']
s = re.split(',|\s', "a1 a2 a3, a4,a5")
getting output = ['a1','a2','a3',' ','a4','a5']
like image 499
Borisw37 Avatar asked Nov 12 '18 20:11

Borisw37


1 Answers

You have to allow one or more split characters:

>>> re.split('[,\s]+', "a1 a2 a3, a4,a5")
['a1', 'a2', 'a3', 'a4', 'a5']
like image 133
Daniel Avatar answered Oct 08 '22 10:10

Daniel