Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to detect if all alphabetic characters are upper case

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!"
like image 791
Evan O. Avatar asked May 23 '18 03:05

Evan O.


People also ask

How do I check for alphabetic characters in regular expressions?

A regular expression for alphabetic characters should check that the expression contains one or more letters of the alphabet in either upper (A to Z) or lower case (a to z), and does not contain any other characters such as numbers or special characters. /^ [A-Za-z]+$/.

Do regular expressions match upper and lowercase characters?

The alphabetic regular expressions shown above will match both upper and lowercase characters or a mix of the two. However, we could also use the case-insensitive flag (i) to indicate that characters in any case can be matched.

How to check if a string contains only upper case characters?

There is function is_upper in Perl, so how can we check if a string contains only upper case characters? The simple thing is to compare it to the upper-case version of itself: but this will also say that "123" us all upper case. Sometimes this is not what you need.

How can I make a string have one upper case letter?

It might not be enough to have one upper case letter. You might require to have all of them: In that case we can use the ^ and $ regex anchors to match the beginning and the end of the string respectively. We also apply the + quantifier that means 1 or more of the preceding thing. In our case one or more of the preceding upper case character.


1 Answers

Reverse your logic

all alphabetic characters are upper case.

is the same as

not a single alphabetic character is lower case.

Code

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.

like image 67
Eric Duminil Avatar answered Sep 28 '22 00:09

Eric Duminil