首页 > 编程语言 >python re模块re.split 以某个字符分割 但保留该字符

python re模块re.split 以某个字符分割 但保留该字符

时间:2023-02-14 10:13:36浏览次数:40  
标签:字符 python slkdjfl sldjfl re split str

代码实现,比如一篇文章以句号、问号、感叹号分句后,把符号带回

import re
 
str_1 = 'sldfl;slkdjfl;sldjfl;sdklf'
str_2 = re.split('([;])',str_1 )  # 注意,这里要用 ([]) 将分隔符 包住
str_2 .append("")
str_2 = ["".join(i) for i in zip(str_2 [0::2],str_2 [1::2])]
print(str_2) 
# ['sldfl;', 'slkdjfl;', 'sldjfl;', 'sdklf']

 

 

参考:

https://zhuanlan.zhihu.com/p/453750062

标签:字符,python,slkdjfl,sldjfl,re,split,str
From: https://www.cnblogs.com/lingwang3/p/17118735.html

相关文章