user5313458
user5313458

Reputation: 23

How do I use regex to match a specific word that contains at least one uppercase letter but not all in lowercase?

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

Answers (3)

Pedro Pinheiro
Pedro Pinheiro

Reputation: 1069

You can use this regex:

\b(?=.*[A-Z])(?i)stackoverflow\b

Demo

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

anubhava
anubhava

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]$");

RegEx Demo

Upvotes: 1

Cody Bouche
Cody Bouche

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

Related Questions