繁体   English   中英

在大写字母之间添加空格,但忽略 3 个字母的单词

[英]Add space in-between Capital Letters but ignore 3-letter words

我一直在寻找一种在大写字母之间添加空格的方法,而忽略了最多 3 个字母的大写单词,例如“CIA”或“FBI”。

例如:

import re

elem = "Optional extrasSafes in guestrooms can be used for EUR 2.5 per nightBabysitting/childcare is available for an extra charge"
elem = re.sub(r"(\w)([A-Z])", r"\1 \2", elem)

print(elem)
elem = "Optional extras Safes in guestrooms can be used for E UR 2.5 per night Babysitting/childcare is available for an extra charge"

在这里,问题是“EUR”已转换为“E UR”。

你知道如何应对吗?

似乎您只想在小写字母后跟大写字母之间的边界处添加一个空格。 如果是这样,那么您可以尝试:

elem = "Optional extrasSafes in guestrooms can be used for EUR 2.5 per nightBabysitting/childcare is available for an extra charge"
output = re.sub(r'([a-z])([A-Z])', r'\1 \2', elem)
print(output)

这打印:

Optional extras Safes in guestrooms can be used for EUR 2.5 per night Babysitting/childcare is available for an extra charge

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM