Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string with two different characters

I have the following string

u'root\n |-- date: string (nullable = true)\n |-- zip: string (nullable = true)\n' 

I would like to extract the column names. The column names have |-- before them and : after them.

I could do this in two stages:

s = u'root\n |-- date: string (nullable = true)\n |-- zip: string (nullable = true)\n' 
s = s.split('|-- ')
s = s.split(':')

However, I wanted to know if there is a way to split with two characters at once.

like image 374
Michal Avatar asked Jun 25 '26 16:06

Michal


1 Answers

However, I wanted to know if there is a way to split with two characters at once.

It is possible using re#split:

re.split(r'\|--|:', your_string)
like image 167
Maroun Avatar answered Jun 27 '26 07:06

Maroun