The part after the @ in an email address is case insensitive so you could convert the entire part after @ to lower case without any problems.
We consider both cases.
Convert everything after last dot to lower case
To use gsubfn
be sure that the regular expression is matching the extension.
Alternately we can make use of file_ext
in the tools package (where the tools package comes with R so no installation is required).
A third approach is to use file_path_sans_ext
together with file_ext
(both from tools) and remove any trailing dot in case there was no extension. If we knew there is always an extension then the sub
part can be omitted.
(Of course if we know that the part before the extension has no upper case characters or if we don't mind that it is converted to lower case we could just apply tolower
to the entire input.)
s <- "something@somewhere.COM"
# 1
library(gsubfn)
gsubfn("\\.[^.]*$", tolower, s)
## [1] "something@somewhere.com"
# 2
library(tools)
ext <- file_ext(s)
sub(paste0(ext, "$"), tolower(ext), s)
## [1] "something@somewhere.com"
# 3
library(tools)
sub("\\.$", "", paste(file_path_sans_ext(s), tolower(file_ext(s)), sep = "."))
## [1] "something@somewhere.com"
Convert everything after @ to lower case
As mentioned earlier the domain, i.e. the portion of the string after the @ character, is case insensitive so we can convert that entire part to lower case. This is a simpler problem as we are guaranteed that there is only one instance of @. We use gsubfn
in (4), extract the parts before and after the @ in (5) and use read.table
without any regular expressions in (6).
s <- "something@somewhere.COM"
# 4
library(gsubfn)
gsubfn("@.*", tolower, s)
## [1] "something@somewhere.com"
# 5
paste(sub("@.*", "", s), tolower(sub(".*@", "", s)), sep = "@")
## [1] "something@somewhere.com"
# 6
with(read.table(text = s, sep = "@", as.is = TRUE), paste(V1, tolower(V2), sep = "@"))
## [1] "something@somewhere.com"