![](/img/trans.png)
[英]How do I insert space before capital letter if and only if previous letter is not capital?
[英]Pythonic Way to Add Space Before Capital Letter If and Only If Previous Letter is Not Also Capital
如标题所示,我想在大写字母之前添加空格,但前提是前一个字母也不是大写字母。 因此, 'HelloCHARLIE this isBob.'
应该变成'Hello CHARLIE this is Bob.'
(?<![A-Z])(?<!^)([A-Z])
print re.sub(r"(?<![A-Z])(?<!^)([A-Z])",r" \1",x)
这可以正常工作,请参见演示。后面使用负号以确保前面的字符不是大写字母或字符串开头。
参见演示。
一种解决方案是
import re
string = 'HelloCHARLIE this isBob.'
re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', string)
版画
'Hello CHARLIE this is Bob.'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.