[英]Pythonic Way to Add Space Before Capital Letter If and Only If Previous Letter is Not Also Capital
As the title says, I would like to add spaces before capital letters, but only if the prior letter is not also a capital letter. 如标题所示,我想在大写字母之前添加空格,但前提是前一个字母也不是大写字母。 So 'HelloCHARLIE this isBob.'
因此, 'HelloCHARLIE this isBob.'
should become 'Hello CHARLIE this is Bob.'
应该变成'Hello CHARLIE this is Bob.'
(?<![A-Z])(?<!^)([A-Z])
print re.sub(r"(?<![A-Z])(?<!^)([A-Z])",r" \1",x)
This works.See demo.Use a negative lookbehind to ensure preceding character is not Capital or start of string. 这可以正常工作,请参见演示。后面使用负号以确保前面的字符不是大写字母或字符串开头。
See demo. 参见演示。
http://regex101.com/r/cH8vN2/1 http://regex101.com/r/cH8vN2/1
One solution would be 一种解决方案是
import re
string = 'HelloCHARLIE this isBob.'
re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', string)
prints 版画
'Hello CHARLIE this is Bob.'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.