我这种情况,我有错点的句子来处理,这句话(。):正则表达式替换为字符串的Python
sentence = 'Hi. Long time no see .how are you ?can you follow [email protected]?'
我试图规范这句话,如果你看到它,有是一些错误的格式句子(.how,?can和。@ abcde)。我正在考虑使用正则表达式来处理这个问题,因为句子不断变化。这是到目前为止我的代码:(。)(?) 进口再
character = ['.','?','@']
sentence = 'Hi. Long time no see .how are you ?can you follow [email protected]?'
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 [email protected]?
它应该是“嗨,好久不见你怎么可以你关注@ ABCDE。?。?”。我不知道“r'\'+ charac + r'\ S *'”中的双反斜杠是错的还是什么,我错过了什么?
如何处理所有角色?请帮忙。
我不知道蟒蛇,但你可能需要正确逃生看到http://stackoverflow.com/questions/280435/escaping-regex-string-in-python – Fallenhero
如果'r'\\''应该转义下一个字符,则只需要使用单个反斜杠。目前你正在逃避反斜杠。但是,您应该使用're.escape'来代替。 –
但我认为你的代码有问题。你会在每一个'。|?| @'之后增加一个空格,这个 – Fallenhero