|  | 
 
 发表于 2019-5-28 08:23:26
|
显示全部楼层
   本楼为最佳答案 
| 复制代码import re
p = re.compile(r'(\b\w+)\s+\1')
print(p.search('Paris in the the spring').group())
'''  
    \b 单词分界符\b  
    \w  代表是大小写字母,10个数字和下划线 这里是是英文字母
    \1,\2…的元字符序列表示前面捕获性括号内的字串(块),”\1”叫反向引用
    \s  任意一个不可见字符,\n\t\r和空格等等  
'''
#稍微严禁一下
p = re.compile(r'\b(\w+)\b\s+\1')
print(p.search('Paris in the the spring').group())
#灵活一下 1
p = re.compile(r'(\w+)\s+\1')
print(p.search('Paris in the the spring').group())
#灵活一下 2
p = re.compile(r'(\w+)\s+\1')
print(p.findall('Paris in the the spring 123 123 abc'))
#['the', '123']
 | 
 |