Regex to detect if all alphabetic characters are upper case

  • A+

How would I go about detecting if all alphabetic characters in a string (of >= 2 characters) are upper case? Ultimately, I'm trying to filter out chapter title names, that are rows in my data-set. So if a chapter title is "ARYA", I want that detected, same as "THE QUEEN'S HAND".

Here's what I'm trying but doesn't work:

library(dplyr) library(stringr)  str_detect("THE QUEEN’S HAND", "^[[:upper:]]{2,}+$") #> FALSE 

The requirements I need:

  • Number of characters >= 2 because I'm ultimately using this to filter out chapter names, but sometimes there's a row where the word is "I", but that's not a chapter -- it's just a word. Though this could be filtered at a different point
  • Only alphabetic characters or apostrophes detected. Sometimes the row is "...", which I don't want detected. However, if I use a toupper(x) == (x) solution, this would be detected alongside something like "THE QUEEN'S HAND". I'm also trying to get rid of anything with exclamation points or periods, like "STOP THIS!"

Reverse your logic

all alphabetic characters are upper case.

is the same as

not a single alphabetic character is lower case.


If you really want to use a regex for this task, all you need to write is :

! str_detect("THE QUEEN’S HAND", "[[:lower:]]") 

You can test it here.

If you want to take the string length into account, you can add a logical OR :

nchar(str) < 2 || ! str_detect(str, "[[:lower:]]") 

You can test it here.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: