使用正则表达式删除大写字母(如果前一个字母和下一个字母是小写字母)?

时间:2019-12-09 17:52:32

标签: python regex string uppercase lowercase

我是新来的正则表达式。我想删除一个大写字母,如果它之前和之后都有小写字母。如果输入为"I wilYl go theXre",则输出应为"I will go there"。我怎么能得到它?

1 个答案:

答案 0 :(得分:1)

您可以使用环顾四周:

import re 

s='I wilYl go theXre'

print(re.sub(r'(?<=[a-z])([A-Z])(?=[a-z])','',s))
#               ^ lookbehind     ^lookahead

打印:

I will go there
相关问题