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
Post a Comment