简体   繁体   English

在Regex Python中查找包含所有大写字母的行

[英]Find the line with all caps in Regex Python

I'm trying to find all lines that are all caps using regex, and so far I've tried this: 我正试图找到所有使用正则表达式全部大写的行,到目前为止我已经尝试过:

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

So far my database is as follows: 到目前为止,我的数据库如下:

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

I want it to return 我想要它回来

TRAFALGAR LAW
MONKEY D LUFFY
DOFLAMINGO'S UNDERLINGS:

But it's returning something else. 但它正在返回别的东西。 (Namely this: (即:

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

EDIT So far I really think the best fit for the answer is @Jan's answer 编辑到目前为止,我真的认为最适合答案的是@ Jan的答案

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

EDIT2 Found out what's wrong, thanks! EDIT2发现了什么问题,谢谢!

Here you go 干得好

import re

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

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

UPPERCASE = [line for line in string.split("\n") if rx.match(line)]
print(UPPERCASE)

Or: 要么:

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

UPPERCASE = rx.findall(string)
print(UPPERCASE)

Both will yield 两者都会屈服

['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]

Brief 简要

No need for regex, python has the method isupper() 不需要正则表达式,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]都是大写且至少有一个外壳字符,则返回true,否则返回false。

[4] Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase). [4]外壳字符是具有一般类别属性的字符,其中一个是“Lu”(字母,大写),“Ll”(字母,小写)或“Lt”(字母,标题)。


Code

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()

Result 结果

True
False
True
False
True
False

You can use [AZ\\W] to check for any uppercase letters along with non alphanumeric characters: 您可以使用[AZ\\W]检查任何大写字母以及非字母数字字符:

import re
s = ["TRAFALGAR LAW", "You shall not be the pirate king.", "MONKEY D LUFFY", "Now!", "DOFLAMINGO'S UNDERLINGS:", "Noooooo!"]
new_s = [i for i in s if re.findall('^[A-Z\d_\W]+$', i)]

Output: 输出:

['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]

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

相关问题 Python 正则表达式:查找所有没有以下小写字母的大写字母 - Python Regex: Find all caps with no following lowercase 使用python中的正则表达式捕获所有连续的全大写单词? - Capture all consecutive all-caps words with regex in python? Python-查找所有正则表达式 - Python - find all regex python - 尝试用同一行但是所有CAPS来替换给定的行 - python - Trying to do a program to replace a given line, by the same line but all CAPS 正则表达式:将所有大写字母与数字分开 - Regex: Separating All Caps from Numbers 正则表达式在所有大写单词之前插入一个字符 - Regex to insert a char before all caps word 使用python“或”过滤名称或命令以查找名称的所有小写字母和所有大写字母版本 - using python “or” to filter names or orders to find all lowcase and all caps versions of the name Python-网络抓取Pubmed摘要-希望在各部分之间创建2个换行符(用大写字母和“:”分隔) - Python - web scraping Pubmed abstract - want to create 2 line breaks between sections (separated by all caps and “:”) Python:如果第一个单词全部大写,则在句子中第一个单词后添加新行 - Python: Add a new line after the first word in a sentence if the first word is all caps Python正则表达式查找所有日期 - Python regex find all dates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM