在Python中,如何拆分字符串并保留分隔符?

在Python中,如何拆分字符串并保留分隔符?

这是最简单的解释方法。我用的是:

re.split('\W', 'foo/bar spam\neggs')-> ['foo', 'bar', 'spam', 'eggs']

我想要的是:

someMethod('\W', 'foo/bar spam\neggs')-> ['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']

原因是我想把一个字符串拆分成令牌,操作它,然后再把它放在一起。


BIG阳
浏览 2277回答 3
3回答

米琪卡哇伊

>>> re.split('(\W)', 'foo/bar spam\neggs')['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']

12345678_0001

另一种在Python 3上运行良好的非正则表达式解决方案#&nbsp;Split&nbsp;strings&nbsp;and&nbsp;keep&nbsp;separatortest_strings&nbsp;=&nbsp;['<Hello>',&nbsp;'Hi',&nbsp;'<Hi>&nbsp;<Planet>',&nbsp;'<',&nbsp;'']def&nbsp;split_and_keep(s,&nbsp;sep): &nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;s:&nbsp;return&nbsp;['']&nbsp;#&nbsp;consistent&nbsp;with&nbsp;string.split() &nbsp;&nbsp;&nbsp;#&nbsp;Find&nbsp;replacement&nbsp;character&nbsp;that&nbsp;is&nbsp;not&nbsp;used&nbsp;in&nbsp;string &nbsp;&nbsp;&nbsp;#&nbsp;i.e.&nbsp;just&nbsp;use&nbsp;the&nbsp;highest&nbsp;available&nbsp;character&nbsp;plus&nbsp;one &nbsp;&nbsp;&nbsp;#&nbsp;Note:&nbsp;This&nbsp;fails&nbsp;if&nbsp;ord(max(s))&nbsp;=&nbsp;0x10FFFF&nbsp;(ValueError) &nbsp;&nbsp;&nbsp;p=chr(ord(max(s))+1)&nbsp; &nbsp;&nbsp;&nbsp;return&nbsp;s.replace(sep,&nbsp;sep+p).split(p)for&nbsp;s&nbsp;in&nbsp;test_strings: &nbsp;&nbsp;&nbsp;print(split_and_keep(s,&nbsp;'<')) &nbsp;&nbsp;&nbsp;#&nbsp;If&nbsp;the&nbsp;unicode&nbsp;limit&nbsp;is&nbsp;reached&nbsp;it&nbsp;will&nbsp;fail&nbsp;explicitlyunicode_max_char&nbsp; &nbsp;&nbsp;&nbsp;=&nbsp;chr(1114111)ridiculous_string&nbsp;=&nbsp;'<Hello>'+unicode_max_char+'<World>'print(split_and_keep(ridiculous_string,&nbsp;'<'))
打开App,查看更多内容
随时随地看视频慕课网APP