gpt4 book ai didi

python - 在 Regex Python 中查找全部大写的行

转载 作者:行者123 更新时间:2023-11-28 20:35:02 25 4
gpt4 key购买 nike

我正在尝试使用正则表达式查找所有大写的行,到目前为止我已经试过了:

re.findall(r'\b\n|[A-Z]+\b', kaizoku)

目前我的数据库如下:

TRAFALGAR LAW
You shall not be the pirate king.
MONKEY D LUFFY
Now!
DOFLAMINGO'S UNDERLINGS:
Noooooo!

我要它回来

TRAFALGAR LAW
MONKEY D LUFFY
DOFLAMINGO'S UNDERLINGS:

但它返回的是其他东西。(即:

TRAFALGAR
LAW
Y
MONKEY
D
LUFFY
N
DOFLAMINGO'
S
UNDERLINGS:
N

编辑到目前为止,我真的认为最适合的答案是@Jan 的答案

rx = re.compile(r"^([A-Z ':]+$)\b", re.M)
rx.findall(string)

EDIT2 找出问题所在,谢谢!

最佳答案

简介

不需要正则表达式,python有方法isupper()

Return true if all cased characters[4] in the string are uppercase and there is at least one cased character, false otherwise.

[4] Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase).


代码

See code in use here

a = [
"TRAFALGAR LAW",
"You shall not be the pirate king.",
"MONKEY D LUFFY",
"Now!",
"DOFLAMINGO'S UNDERLINGS:",
"Noooooo!",
]

for s in a:
print s.isupper()

结果

True
False
True
False
True
False

关于python - 在 Regex Python 中查找全部大写的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683871/

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