我想从字符串中删除首字母缩略词中的句点,但我也希望o定期(例如在句子结尾)保留句号。正则表达式来删除首字母缩略词中的句点?
所以下面的句子:
"The C.I.A. is a department in the U.S. Government."
应该成为
"The CIA is a department in the US Government."
有没有干净的方式来做到这一点使用Python?到目前为止,我有两个步骤:
words = "The C.I.A. is a department in the U.S. Government."
words = re.sub(r'([A-Z].[A-Z.]*)\.', r'\1', words)
print words
# The C.I.A is a department in the U.S Government.
words = re.sub(r'\.([A-Z])', r'\1', words)
print words
# The CIA is a department in the US Government.
可以有任何单字母缩写吗? –
'etc.'呢?如果你只关心大写单个字母后跟一个点,你可以使用're.sub(r'\ b([AZ])\。',r'\ 1',words)',但这不是一般的解。 –
归结到这个问题*“你怎么知道什么是一个缩略语而不是一个句子?”*一旦你有了答案,那么你就可以开始构建一个正则表达式。 – zvone