Skip to main content
 首页 » 编程设计

python之在 ". ","! "或 " "上拆分字符串,保留标点符号

2025年05月04日14JustinYoung

<分区>

Possible Duplicate:
Python split() without removing the delimiter

我想拆分一个字符串如下:

text = " T?e  qu!ck ' brown 1 fox!     jumps-.ver. the 'lazy' doG?  !" 
result -> (" T?e  qu!ck ' brown 1 fox!", "jumps-.ver.", "the 'lazy' doG?", "!") 

所以基本上我想在 ".", "!" 或 "? " 处拆分,但我想要拆分处的空格要删除的点,但不删除点、逗号或问号。

我怎样才能有效地做到这一点?

str split 函数只接受分隔符。我想知道最好的解决方案是拆分所有空格,然后在构造所需结果时找到以点、逗号或问号结尾的空格。