I have several strings (some values may differ but format is ultimately the same):
"I Suck At Regex: 100.00 WHATIWANT Plz Help: Thank you: 200.00"
"I Suck At Regex: 200.00 WHATIWANT Plz Help: Thank you: 200.00"
"I Suck At Regex: 300.00 ALSOWANT Plz Help: Thank you: 300.00"
I would like to extract the substring 'WHATIWANT' from the full string
I tried using [A-Z] to only extract capitalized letters, however, as you can tell, my string has capitalized letters in other positions
CodePudding user response:
How about this:
import re
strings = [
"I Suck At Regex: 100.00 WHATIWANT Plz Help: Thank you: 200.00",
"I Suck At Regex: 200.00 WHATIWANT Plz Help: Thank you: 200.00",
"I Suck At Regex: 300.00 ALSOWANT Plz Help: Thank you: 300.00"
]
# match a sequence of letters in the range A-Z with a length of at least 2
p = re.compile(r'\b([A-Z]{2,})\b')
for string in strings:
print(*p.findall(string))
Output:
WHATIWANT
WHATIWANT
ALSOWANT
CodePudding user response:
You can do like this (it will match your substring):
input_string = "I Suck At Regex: 100.00 WHATIWANT Plz Help: Thank you: 200.00"
pattern = "WHATIWANT"
match = re.findall(pattern, input_string)
if match:
print(match[0])