Extracting text after last period in string [duplicate]
I realize this question probably seems painfully simple to most regular expression masters, but reviewing similar questions has not yielded a solution.
I have a vector of e-mail addresses called email
and would like to extract the text after the final period in each one. For the sake of example,
email<-c("[email protected]", "[email protected]", "[email protected]")
I have tried:
grep("[\.][a-zA-Z]*?$", email, value=T)
This gets me the error message:
Error: '.' is an unrecognised escape in character string starting ""."`
Removing the escape character on the other hand
grep("[.][a-zA-Z]*?$", email, value=T)
returns the entire e-mail address as does:
grep("\\.[a-zA-Z]*$", email, perl=T, value=T)
I'd really appreciate help at this point.
If you need to extract the string after the last period (.
), try with sub
sub('.*\\.', '', email)
#[1] "com" "com"
data
email <- c('[email protected]', 'xxx$xxxx.com')
Try
email <- c("[email protected]", "[email protected]")
sapply(strsplit(email, split= ".", fixed = TRUE), tail, 1L)
# [1] "com" "com"
Also, as pointed out by @RichardScriven, tools
has a tailor-made function for what you're trying to do specifically:
library(tools)
file_ext(email)
# [1] "com" "com"