2017-10-10 47 views

回答

0
import re 
s = your string here \nhello" re.split('\s+', s) 

通過re模塊的另一種方法。

>>> import re 
>>> s = "your string here \nhello \thi" 
>>> re.findall(r'\S+', s) ['your', 'string', 'word', 'hello', 'hi'] 

這將匹配一個或多個非空格字符。

0

試試這個:

s ='Hi what are you doing?' 
import re 
re.findall('[a-zA-Z]{1,}|[^a-zA-Z]{1,}', s) 

輸出:

['Hi', ' ', 'what', ' ', 'are', ' ', 'you', ' ', 'doing', '?'] 
2

如果你想只拆分:

String1='Hi what are you doing ?' 
print(String1.split()) 

輸出:

['Hi', 'what', 'are', 'you', 'doing', '?'] 
如果你想爲你的例子所示

print(String1.replace(" "," \s ").split()) 

輸出:

['Hi', '\\s', 'what', '\\s', 'are', '\\s', 'you', '\\s', 'doing', '\\s', '?'] 
相關問題