How do you split a string in Python with multiple delimiters? -


for example, want split "hello>>>world!!!!2]]splitting" ["hello", "world","2","splitting"]. doesn't need ^that^, want split string multiple (say 5) delimiters. thanks.

edit: want keep delimiter, making ["hello", ">>>", "world", "!!!!", "2", "]]", "splitting"]

here's i've tried:

>>> string = "hello>>>world!!!!2]]splitting" >>> import re >>> re.split("(\w)>>>|!!!!|]]", string) ['hello>>>world', none, '2', none, 'splitting'] 

(i'm new @ regex)

to using re.split can do:

re.split(r'(>+|!+|]+)', string) 

explaining briefly:

  • you split on 1 or more occurrences of different delimiters (>, !, ]).
  • in order include delimiters in result, put pattern in capturing group putting parens around it.

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -