I had always heard that the [:alpha:] character class was equivalent to [A-Za-z] but in the output below this does not seem to be the case regardless if I use gsub (with and without perl = TRUE) or stringi. It seems [:alpha:] matches non-ascii characters but I may be misunderstanding. Using ?regex tells me:
Certain named classes of characters are predefined. Their interpretation depends on the locale (see locales); the interpretation below is that of the POSIX locale.
But I still don't get the difference. To me [A-Za-z] matches exactly 52 characters, while [:alpha:] matches way more.
Questions
- What is the difference between
[:alpha:]class and[a-zA-Z] - What exactly will
[:alpha:]match? - Will
[:alpha:]work the same across different operating systems and locations around the world?
x <- c(
"danish characteøs sentåment æcores words correctly 456",
"It works with probleme but not with problème 234"
)
gsub("[[:alpha:] ]", '', x)
## "456" "234"
gsub("[a-zA-Z ]", '', x)
## [1] "øåæ456" "è234"
stringi::stri_replace_all_regex(x, "[[:alpha:] ]", '')
## "456" "234"
stringi::stri_replace_all_regex(x, "[a-zA-Z ]", '')
## [1] "øåæ456" "è234"