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:
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!"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]+$/.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With