<p>这应该适用于“divLineColor”、“simpleBigURL”、“OldHTMLFile”和“SQLServer”。</p>
<pre><code>label = re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', label)
</code></pre>
<p>说明:</p>
<pre><code>label = re.sub(r"""
( # start the group
# alternative 1
(?<=[a-z]) # current position is preceded by a lower char
# (positive lookbehind: does not consume any char)
[A-Z] # an upper char
#
| # or
# alternative 2
(?<!\A) # current position is not at the beginning of the string
# (negative lookbehind: does not consume any char)
[A-Z] # an upper char
(?=[a-z]) # matches if next char is a lower char
# lookahead assertion: does not consume any char
) # end the group""",
r' \1', label, flags=re.VERBOSE)
</code></pre>
<p>如果找到匹配项,则将其替换为<code>' \1'</code>,该字符串由前导空格和匹配项本身组成。</p>
<p>匹配的替代项1是一个大写字符,但前提是它前面有一个小写字符。我们想把<code>abYZ</code>翻译成<code>ab YZ</code>,而不是<code>ab Y Z</code>。</p>
<p>匹配的替代项2是一个大写字符,但前提是它后面跟一个小写字符,而不是字符串的开头。我们想把<code>ABCyz</code>翻译成<code>AB Cyz</code>,而不是<code>A B Cyz</code>。</p>