Validating email adresses in R

July 5, 2012
By

(This article was first published on Nicebread » R, and kindly contributed to R-bloggers)

I currently program an automated report generation in R – participants fill out a questionnaire, and they receive a nicely formatted pdf with their personality profile. I use knitr, LaTex, and the sendmailR package.

Some participants did not provide valid email addresses, which caused the sendmail function to crash. Therefore I wanted some validation of email addresses – here’s the function:

?View Code RSPLUS
isValidEmail <- function(x) {
	grepl("\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>", as.character(x), ignore.case=TRUE)
}

Let’s test some valid and invalid adresses:

?View Code RSPLUS
# Valid adresses
isValidEmail("[email protected]")
isValidEmail("[email protected]")
isValidEmail("[email protected]  ")
isValidEmail("    [email protected]")
isValidEmail("[email protected]")
isValidEmail("[email protected]")
 
# invalid addresses
isValidEmail("felix@nicebread")  
isValidEmail("felix@nicebread@de")
isValidEmail("felixnicebread.de")

The regexp is taken from www.regular-expressions.info and adapted to the R style of regexp. Please note the many comments (e.g., here or here) about “Is there a single regexp that matches all valid email adresses?” (the answer is no).

To leave a comment for the author, please follow the link and comment on his blog: Nicebread » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.