How can I remove space before and after @?
For example,
safety@ gmail.com / [email protected]
gjhv_mf6 @ hotmail.com,hhty @gmail.com
the desired output will be:
[email protected] / [email protected]
[email protected],[email protected]
How can I remove space before and after @?
For example,
safety@ gmail.com / [email protected]
gjhv_mf6 @ hotmail.com,hhty @gmail.com
the desired output will be:
[email protected] / [email protected]
[email protected],[email protected]
gsub()
should do it.
string_vec <- c("safety@ gmail.com / [email protected]",
"gjhv_mf6 @ hotmail.com,hhty @gmail.com")
gsub(" *@ *","@",string_vec)
If you want to remove all whitespace (including tabs etc.), follow this question:
gsub("[[:space:]]*@[[:space:]]*", "@", string_vec)
Another option it to remove optional whitespace before and after "@"
.
Using @BenBolker's data
gsub("\\s?@\\s?", "@", string_vec)
#[1] "[email protected] / [email protected]" "[email protected],[email protected]"
OR with stringr::str_replace_all
stringr::str_replace_all(string_vec, "\\s?@\\s?", "@")