查找缩写但不是部分大写的单词

时间:2013-06-19 01:55:04

标签: python regex string

如何找到所有可能的缩写词,如CPR和S.O.S.正则表达式= [A-Z] [A-Z] +可以找到带帽的所有单词,但我想排除像REgex这样的单词。

line2 = "What does CPR and S.O.S means ?"

match = re.search(r'[A-Z][A-Z]+', line2)  
if match:                      
    print 'found', match.group() 
else:
    print 'did not find'

编辑:根据建议将问题分成两个单独的帖子。

2 个答案:

答案 0 :(得分:4)

>>> line2 = "What does CPR and S.O.S means ?"
>>> re.findall(r'\b[A-Z\.]+\b', line2)
['CPR', 'S.O.S']

答案 1 :(得分:0)

试用正则表达式[A-Z\.]+\s。但是,它在缩写后需要空格。