简体   繁体   English

当且仅当前一个字母也不也是大写的Pythonic方式才能在大写字母前添加空间

[英]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.

相关问题 当且仅当前一个字母不是大写字母时,才如何在大写字母前插入空格? - How do I insert space before capital letter if and only if previous letter is not capital? 在大写字母前插入空格的pythonic方法 - A pythonic way to insert a space before capital letters 如何在大写字母前添加新行? - How to add a new line before a capital letter? Pythonic句子拆分以大写字母开头的单词 - Pythonic sentence splitting on words starting with capital letter 如何在没有空格的情况下对大写字母进行正则表达式| Python 3 - How to regex Capital Letter without space before | Python 3 在大写字母之间添加空格,但忽略 3 个字母的单词 - Add space in-between Capital Letters but ignore 3-letter words 查找以大写字母作为起始字母但前面没有空格的单词 - find words with capital letter as starting letter but not preceded by space 以大写字母分割字符串,但仅限于没有空格 - Split string at capital letter but only if no whitespace 如何构建这个正则表达式,以便它提取一个以大写字母开头的单词,前提是它出现在前一个模式之后? - How to build this regex so that it extracts a word that starts with a capital letter if only if it appears after a previous pattern? 识别罗马数字后跟“.”、空格和大写字母。 (正则表达式) - Recognize roman numeral followed by '.', space and then capital letter. (RegEx)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM