在大写字母之间添加 space 但忽略 3 个字母的单词
Add space in-between Capital Letters but ignore 3-letter words
我一直在寻找一种方法在大写字母之间添加 space,同时忽略最多 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" ", 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”。
你知道如何应对吗?
您似乎只想在 小写 后跟 大写 字母之间的边界处添加 space .如果是这样,那么您可以尝试:
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' ', 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
我一直在寻找一种方法在大写字母之间添加 space,同时忽略最多 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" ", 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”。
你知道如何应对吗?
您似乎只想在 小写 后跟 大写 字母之间的边界处添加 space .如果是这样,那么您可以尝试:
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' ', 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