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
rx = re.compile(r"^([A-Z ':]+$)\b", re.M)
rx.findall(string)
EDIT2 Found out what's wrong, thanks!
No need for regex, python has the method 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
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:"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With