12345678_0001
另一种在Python 3上运行良好的非正则表达式解决方案# Split strings and keep separatortest_strings = ['', 'Hi', ' ', '<', '']def split_and_keep(s, sep):
if not s: return [''] # consistent with string.split()
# Find replacement character that is not used in string
# i.e. just use the highest available character plus one
# Note: This fails if ord(max(s)) = 0x10FFFF (ValueError)
p=chr(ord(max(s))+1)
return s.replace(sep, sep+p).split(p)for s in test_strings:
print(split_and_keep(s, '<'))
# If the unicode limit is reached it will fail explicitlyunicode_max_char
= chr(1114111)ridiculous_string = ''+unicode_max_char+''print(split_and_keep(ridiculous_string, '<'))