使用re.sub反向引用,我们可以尝试:
inp = 'Hey there There'
output = re.sub(r'(\w+) \1', r'\1', inp, flags=re.IGNORECASE)
print(output) # Hey there
Run Code Online (Sandbox Code Playgroud)
此处使用的正则表达式模式表示:
inp = 'Hey there There'
output = re.sub(r'(\w+) \1', r'\1', inp, flags=re.IGNORECASE)
print(output) # Hey there
Run Code Online (Sandbox Code Playgroud)
然后,我们只用第一个相邻的单词替换。
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |