how to split sentences with multiple splitting parameters and including split elements [duplicate]

StackOverflow https://stackoverflow.com/questions/23631949

I have a text suppose 'this is my a,b,c,and d.'

I want a list corresponding to the text as

['this',' ','is',' ',my,' ','a',',','b',',','c',' ','and',' ','d','.'] .

The present split() method eliminated element by which it is splitting and it does not support multiple splitting parameters.

I want list from a string spliced with respect to special characters including the special characters in the list. What is the simplest way to achieve it.

有帮助吗?

解决方案

You can find all the consecutive word characters and the non-word characters. That should give you the following result.

data = 'this is my a,b,c,and d.'
import re
print re.findall(r"\w+|\W+", data)
# ['this', ' ', 'is', ' ', 'my', ' ', 'a', ',', 'b', ',', 'c', ',', 'and', ' ', 'd', '.']
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top