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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

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