Reputation: 23
I want to match one specific word "StackOverflow", "STACKOVERFLOW", or "stACKoverFlow" etc. At least one character should be capitalized, but the word with all lower-case "stackoverflow" should not be matched.
Thank you very much for your help.
Upvotes: 2
Views: 2850
Reputation: 1069
You can use this regex:
\b(?=.*[A-Z])(?i)stackoverflow\b
In python would be like:
import re
p = re.compile(ur'\b(?=.*[A-Z])(?i)stackoverflow\b')
test_str = u"StackOverflow\nSTACKOVERFLOW\nstACKoverFlow\nstackoverflow\n"
re.findall(p, test_str)
Upvotes: 0
Reputation: 784998
Using (?i)
in the middle of the regex won't work in Python as (?i)
will impact the entire regex:
You will have to use this longish regex:
regex = re.compile(r"^(?!stackoverflow$)[sS][tT][aA][cC][kK][oO][vV][eE][rR][fF][lL][oO][wW]$");
Upvotes: 1
Reputation: 955
This uses word boundaries to ensure one word and checks for at least one capital letter
import re
text = 'StackOverflow STACKOVERFLOW stACKoverFlow stackoverflow'
matches = re.findall(r'\b.*?[A-Z]{1}.*?\b', text)
#['StackOverflow', ' STACKOVERFLOW', ' stACKoverFlow']
This is how to do this without regex. It will match exclusively "stackoverflow" as long as it has at least on capital letter
text = 'StackOverflow STACKOVERFLOW stACKoverFlow stackoverflow'
matches = [word for word in text.split() if any(letter.isupper() for letter in word) and word.lower() == 'stackoverflow']
#['StackOverflow', 'STACKOVERFLOW', 'stACKoverFlow']
Upvotes: 0