根据多个定界符拆分字符串,同时保留它们

Split strings based on multiple delimiters while retaining them as well

我正在输入 5 个句子,需要使用多个分隔符 (,/!/?) 将它们拆分

不幸的是,在编写代码时我只考虑了字母并放置了这些分隔符并使用了 .split()。当时还好好的。

这是代码:

final_text = ''
split_one = ''
input_text = input("Enter the data: ")
count_d = input_text.count("!") + input_text.count("?") + input_text.count(".")

if count_d == 5:
            final_text = input_text
            final_text = final_text.replace('!', '! ').replace('?', '? ').replace('.', '. ')
            split_one = final_text.split()
            i = 0
            while True:
                print(split_one[i])
                i += 1
                if i == 5:
                    break

对于输入:a.b?c!d.f!

The output was 
a.
b?
c!
d.
f!

但实际上我输入的是句子而不是字母。例如

hi.how are you? I am good! what about you?bye!

它给了我:

 hi.
 how
 are
 you?
 I

而不是

hi.
how are you?
I am good!
what about you?
bye!

我该怎么做才能避免因空格而导致的拆分并仅针对分隔符进行拆分? (,/./!)

PS:我不会使用任何外部包。版本是 3.6

您可以使用itertools.groupby来按标点分割字符串,例如:

>>> import itertools as it
>>> s = 'hi.how are you? I am good! what about you?bye!'
>>> r = [''.join(v).strip() for k, v in it.groupby(s, lambda c: c in '.!?')]
>>> r
['hi', '.', 'how are you', '?', 'I am good', '!', 'what about you', '?', 'bye', '!']
>>> for sentence, punct in zip(*[iter(r)]*2):
...     print(sentence + punct)
hi.
how are you?
I am good!
what about you?
bye!

如果您不在意标点符号,那么您可以使用:

>>> [''.join(v).strip() for k, v in it.groupby(s, lambda c: c in '.!?') if not k]
['hi', 'how are you', 'I am good', 'what about you', 'bye']