12/10/2017

Python: Split string with multiple delimiters [duplicate]


String split using delimiter

let's see how to split string using delimiter
There are 2 issues those are single delimiter and multi delimiters.

import re
#single delimiter
inputStr = 'This\nis\nmy\ndesk.'
split_inpuStr = inputStr.split('\n')
print('single',split_inpuStr)

#double delimiters
inputStr2 = 'This\r\nis\nmy\ndesk.'
split_inpuStr2 = re.split('\r\n|\n', inputStr2)
print('double',split_inpuStr2)

#multi delimiters
a = 'Beautiful,is; better*than\nugly'
b = re.split('; |, |\*|\n',a)
print('multi',b)


reference

https://stackoverflow.com/questions/4998629/python-split-string-with-multiple-delimiters
https://stackoverflow.com/questions/10393157/splitting-a-string-with-multiple-delimiters-in-python

python regular auto generator
http://ss2r.marearts.com

No comments:

Post a Comment