我在专栏中有以下模式
xyz@gmail.com
abc@hotmail.com
Run Code Online (Sandbox Code Playgroud)
现在,我想@在.gmail和hotmail 之前和之后提取文本。我可以.使用以下代码提取文本。
sub(".*@", "", email)
Run Code Online (Sandbox Code Playgroud)
如何在上面进行修改以适合我的用例?
您:
@可以出现在多个位置)someone@department.example.com”,“ someone.else@yet.another.department.example.com”(即,在此分析中的某个时候,天真的假设只有一个域可能会再次咬住您)因此- 除非您确定自己拥有并且始终会有简单的电子邮件地址,否则我建议:
library(stringi)
library(urltools)
library(dplyr)
library(purrr)
emails <- c("yz@gmail.com", "abc@hotmail.com",
"someone@department.example.com",
"someone.else@yet.another.department.com",
"some.brit@froodyorg.co.uk")
stri_locate_last_fixed(emails, "@")[,"end"] %>%
map2_df(emails, function(x, y) {
substr(y, x+1, nchar(y)) %>%
suffix_extract()
})
## host subdomain domain suffix
## 1 gmail.com <NA> gmail com
## 2 hotmail.com <NA> hotmail com
## 3 deparment.example.com department example com
## 4 yet.another.department.com yet.another department com
## 5 froodyco.co.uk <NA> froodyorg co.uk
Run Code Online (Sandbox Code Playgroud)
请注意子域,域和后缀的正确分割,尤其是对于最后一个。
知道这一点后,我们可以将代码更改为:
stri_locate_last_fixed(emails, "@")[,"end"] %>%
map2_chr(emails, function(x, y) {
substr(y, x+1, nchar(y)) %>%
suffix_extract() %>%
mutate(full_domain=ifelse(is.na(subdomain), domain, sprintf("%s.%s", subdomain, domain))) %>%
select(full_domain) %>%
flatten_chr()
})
## [1] "gmail" "hotmail"
## [3] "department.example" "yet.another.department"
## [5] "froodyorg"
Run Code Online (Sandbox Code Playgroud)
我们可以用gsub
gsub(".*@|\\..*", "", email)
#[1] "gmail" "hotmail"
Run Code Online (Sandbox Code Playgroud)