gpt4 book ai didi

python - 正则表达式除以大写

转载 作者:太空狗 更新时间:2023-10-30 02:14:20 25 4
gpt4 key购买 nike

我想用正则表达式将 'HDMWhoSomeThing' 之类的字符串替换为 'HDM Who Some Thing'

所以我想提取以大写字母开头或仅由大写字母组成的单词。请注意,在字符串 'HDMWho' 中,最后一个大写字母实际上是单词 Who 的第一个字母 - 不应包含在单词 中>HDM.

实现此目标的正确正则表达式是什么?我尝试了许多类似于 [A-Z][a-z]+ 的正则表达式,但没有成功。 [A-Z][a-z]+ 给了我 'Who Some Thing' - 当然没有 'HDM'

有什么想法吗?谢谢,琉璃

最佳答案

#! /usr/bin/env python

import re
from collections import deque

pattern = r'([A-Z]{2,}(?=[A-Z]|$)|[A-Z](?=[a-z]|$))'
chunks = deque(re.split(pattern, 'HDMWhoSomeMONKEYThingXYZ'))

result = []
while len(chunks):
buf = chunks.popleft()
if len(buf) == 0:
continue
if re.match(r'^[A-Z]$', buf) and len(chunks):
buf += chunks.popleft()
result.append(buf)

print ' '.join(result)

输出:

HDM Who Some MONKEY Thing XYZ

Judging by lines of code, this task is a much more natural fit with re.findall:

pattern = r'([A-Z]{2,}(?=[A-Z]|$)|[A-Z][a-z]*)'
print ' '.join(re.findall(pattern, 'HDMWhoSomeMONKEYThingX'))

输出:

HDM Who Some MONKEY Thing X

关于python - 正则表达式除以大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com