Home > Back-end >  What is the regex for searching a word only if it is in All Capital letter?
What is the regex for searching a word only if it is in All Capital letter?

Time:02-23

I have a string who did DID what ? what can I add to the below regex expression to search only if DID is in capital letters ?

output =  re.findall(
            r"did", # not adding DID here as the output maycome with did or DID but i need to check only when its all caps
            searchstring,
        )

CodePudding user response:

You can find all CAPS letter word this way:

import re

txt = "who did DID what Did DiD?"
result = re.findall(r"\b[A-Z] \b", txt)
print(result)

Output:

['DID']

CodePudding user response:

I have a string who DID what ? what can I add to the below regex expression to search only if DID is in capital letters ?

output =  re.findall(
            r"did",
            searchstring,
        )

If you want to search for capital letters just search for capital letters:

output =  re.findall(
            r"DID",
            searchstring,
        )
  • Related