python findall匹配全部大写的字符串

时间:2017-10-09 18:26:59

标签: python

我正在尝试使用python re.findall来匹配以下条件的字符串:

contain all uppercase letters
have numbers present sometimes

我试过

ab_list = re.findall(r'([A-Z]+)(\.\d+)', text)

但这不会返回任何内容。

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

import re
s = ["ALL CAPITALS ARE ON", "Some lower, soMe not", "AGAIN, WITH PUNCTIONATION."]
final_data = [i for i in s if re.findall("^[A-Z0-9\W]+$", i)]

输出:

['ALL CAPITALS ARE ON', 'AGAIN, WITH PUNCTIONATION.']

如果您要查找全部大写的单词:

s = ["NOT (LOWER)", "Some lower, soMe not", "AGAIN, WITH PUNCTIONATION.", "young 10 (MODY10)"]
final_data = [b for b in [re.findall("(?<=\().*?(?=\))", i) for i in s] if b and re.findall("^[A-Z0-9\W]+$", b[0])]
final_data = [b for b in [re.findall("\(([A-Z0-9\W])\)", i) for i in s] if b]

输出:

[['LOWER'], ['MODY10']]

如果您有一个长字符串:

s = 'NOT (LOWER)Some lower, soMe notAGAIN, WITH PUNCTIONATION.young 10 (MODY10)'
final_strings = re.findall("\(([A-Z0-9\W]+)\)", s)

输出:

['LOWER', 'MODY10']

答案 1 :(得分:1)

这将找到仅包含大写字母,数字和空格的字符串。

re.findall('^[A-Z0-9 ]+$', text)

如果匹配则返回包含字符串的列表,否则返回空列表。但是,如果您只想检测整个字符串是否匹配,则使用re.match而不是re.findall可能更为直接。这取决于你最终想做什么。

如果您想要找到单个单词,您可能需要:

re.findall('([A-Z0-9]+)', text)