我想替换dot /? /!然后间隔(如果有的话)到断裂线char \n
并消除空格。
因此,如果:hello world. It's nice.
我希望它为hello world.\nIt'snice.\n
这就是我的想法(但它不起作用,否则我不会写这个问题哈哈?)
re.sub(r'\.!?( *)', r'.\n\1', line)
谢谢!
答案 0 :(得分:3)
没有环视:
>>> import re
>>> line="hello world! What? It's nice."
>>> re.sub(r'([.?!]+) *', r'\1\n', line) # Capture punctuations; discard spaces
"hello world!\nWhat?\nIt's nice.\n"
>>> line="hello world! His thoughts trailed away... What?"
>>> re.sub(r'([.?!]+) *', r'\1\n', line)
'hello world!\nHis thoughts trailed away...\nWhat?\n'
答案 1 :(得分:2)
将空格或字符串的结尾与正面后视匹配:
re.sub(r'(?<=[.?!])( +|\Z)', r'\n', text)
因为这只匹配标点符号前面的空格,所以不需要使用反向引用。
+
确保此处仅匹配空格后跟空格。文字:
"His thoughts trailed away... His heart wasn't in it!"
否则会收到太多的换行符。
演示:
>>> import re
>>> text = "hello world. It's nice."
>>> re.sub(r'(?<=[.?!])( +|\Z)', r'\n', text)
"hello world.\nIt's nice.\n"
>>> text = "His thoughts trailed away... His heart wasn't in it!"
>>> re.sub(r'(?<=[.?!])( +|$)', r'\n', text)
"His thoughts trailed away...\nHis heart wasn't in it!\n"
答案 2 :(得分:0)
你试过replace
吗?
print text.replace('. ','.\n').replace('? ','?\n').replace('! ','!\n')