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