如果这些特殊字符坚持单词,如何在Python中创建RE以在这些特殊字符,?!
前添加空白?
这是输入字符串:
myString= 'I like him, but is he good? Maybe he is good , smart, and strong.'
所需的输出(如果特殊字符没有粘贴到某个单词,则不会被修改):
modifiedString= 'I like him , but is he good ? Maybe he is good , smart , and strong.'
我试过这个:
modifiedString= re.sub('\w,' , ' ,' ,myString)
但它给出了错误的结果。它删除昏迷前的最后一个字符,这里是结果示例:
modifiedString = 'I like hi , but is he good? Maybe he is goo , smar , and strong.'
有任何解决这个问题的建议吗?
答案 0 :(得分:3)
您可以使用re.sub
:
>>> import re
>>> myString= 'I like him, but is he good? Maybe he is good , smart, and strong.'
>>> re.sub('(?<=\w)([!?,])', r' \1', myString)
'I like him , but is he good ? Maybe he is good , smart , and strong.'
>>>
(?<=\w)
是一个与单词字符匹配的回顾断言。
([!?,])
是一个匹配字符集[!?,]
的捕获组(您可以在方括号内添加任何您想要匹配的字符)。
\1
是指([!?,])
抓取的文字。
答案 1 :(得分:0)
事情是你也在替换角色。您需要在regex中使用组保留它,然后在替换字符串中指定组号。
>>> myString = 'I like him, but is he good? Maybe he is good , smart, and strong.'
>>> re.sub(r'(\w)([,?!])' , r'\1 \2' ,myString)
'I like him , but is he good ? Maybe he is good , smart , and strong.'
答案 2 :(得分:0)
作为替代答案,您可以在没有正则表达式的情况下解决问题,只需使用str.replace
:
>>> rep_list=['?',',','!']
>>> for i in rep_list :
... if i in myString:
... myString=myString.replace(i,' '+i)
...
>>> myString
'I like him , but is he good ? Maybe he is good , smart , and strong.'