为什么我的re.sub不使用我的正则态度来查找所有实例?
我在 Windows 10 上使用 Python 3.10 并尝试以下搜索:
re.sub(r'(.*[A-Z]+[a-z]+)([A-Z])', r'\1 \2', 'JohnnyB Cool & JoeCool')
'JohnnyB Cool & Joe Cool'
如果我仅使用“JohnnyB Cool”,则“B”前面会出现一个空格。
re.sub(r'(.*[A-Z]+[a-z]+)([A-Z])', r'\1 \2', 'JohnnyB Cool')
'Johnny B Cool'
为什么第一次搜索中没有替换“JohnnyB”?我也尝试过:
re.sub(r'(.*)([A-Z]+[a-z]+)([A-Z])', r'\1 \2 \3', 'JohnnyB Cool & JoeCool')
'JohnnyB Cool & Joe Cool'
要明确的是,我希望最终答案是,Johnny B Cool &乔酷。
I'm using Python 3.10 on Windows 10 and trying the search below:
re.sub(r'(.*[A-Z]+[a-z]+)([A-Z])', r'\1 \2', 'JohnnyB Cool & JoeCool')
'JohnnyB Cool & Joe Cool'
If I use just "JohnnyB Cool", the "B" gets a space before it.
re.sub(r'(.*[A-Z]+[a-z]+)([A-Z])', r'\1 \2', 'JohnnyB Cool')
'Johnny B Cool'
Why isn't the "JohnnyB" substituted in the first search? I've also tried:
re.sub(r'(.*)([A-Z]+[a-z]+)([A-Z])', r'\1 \2 \3', 'JohnnyB Cool & JoeCool')
'JohnnyB Cool & Joe Cool'
To be clear, I want the final answer to be, Johnny B Cool & Joe Cool
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下 python 代码:
RegEx 演示
说明:
\B
匹配\b
不匹配的地方,即与另一个单词字符相邻的[AZ]
匹配大写字母You may use this python code:
RegEx Demo
Explanation:
\B
matches where\b
doesn't i.e. adjacent to another word character[A-Z]
matches an uppercase letter