带替换字符串的正则表达式 Python

Regex with Replace String Python

我有这种情况,我有一个句点(.)错了要处理,句子:

sentence = 'Hi. Long time no see .how are you ?can you follow .@abcde?'

我正在尝试规范化这句话,如果你看到它,就会发现有一些格式错误的句子(.how、?can 和 .@abcde)。我正在考虑使用正则表达式来处理这个问题,因为句子不断变化。到目前为止,这是我的代码: 导入重新

character = ['.','?','@']

sentence = 'Hi. Long time no see .how are you ?can you follow .@abcde?'

sentence = str(sentence)
for i in character:
    charac = str(i)
    charac_after = re.findall(r'\'+charac+r'\S*', sentence)
    if charac_after:
        print("Exist")
        sentence = sentence.replace(charac, charac+' ')

print(sentence)

结果有些是如何跳过点 (.) 和 (@) 它只处理问号 (?)。这是结果: 存在

Hi. Long time no see .how are you ? can you follow .@abcde?

它应该是 "Hi. Long time no see . how are you ? can you follow . @ abcde?"。我不知道我在 "r'\'+charac+r'\S*'" 中的双反斜杠是错误的还是什么,我错过了什么吗?

如何处理所有字符?请帮忙

在不了解 python 的情况下,我认为您需要这样做:

(根据@Sebastian Proske 的建议)

character = ['.','?','@']
sentence = str('Hi. Long time no see .how are you ?can you follow .@abcde?')
sentence = re.sub(r'([' + ''.join(map(re.escape, character)) + r'])(?=\S)', r' ', sentence)
print(sentence)

我不确定代码,但是正则表达式。看这里: https://regex101.com/r/HXdeuK/2

在此处查看演示 https://repl.it/Fw5b/3