如何根据电子邮件地址后缀筛选表

How to filter a table based on email address suffix

提问人:Assa Yeroslaviz 提问时间:11/3/2023 最后编辑:zx8754Assa Yeroslaviz 更新时间:11/3/2023 访问量:62

问:

我有一个包含超过 100K 个姓名和地址的表格。我想过滤表格,只保留那些我认为不是垃圾邮件的电子邮件。

例如,我有这样的地址

[email protected]
[email protected]
[email protected]

我现在想过滤那些在符号之前只有数字的地址,以及那些在 之后但在后缀之前只有数字的电子邮件。@@.com

我知道我可以使用 和 提取它们,但我无法将它们放入查询中以将它们从表中删除。str_splitgreplfilter

pattern <- "[email protected]"
str_split(pattern, '@') # this will split the address based on the sumbol

str_split(string = str_split(pattern, '@')[[1]][2], pattern = "\\.") # this will split the doamin name based on the dot separating the suffix from the numbers.

as.numeric(str_split(string = str_split(pattern, '@')[[1]][2], pattern = "\\.")[[1]][1]) # This for example will check if the string extracted above contains only numbers, if not it will return NA

但是,如何在查询中组合它呢?tidyverse

谢谢

附言 我知道这是一个牵强附会的问题,但是是否有某种可以在 R 中使用电子邮件地址的垃圾邮件过滤器?

R 正则表达式 过滤器 Tidyverse 垃圾邮件

评论


答:

3赞 Ronak Shah 11/3/2023 #1

我认为这种模式应该可以帮助您根据自己的情况识别垃圾邮件。

^\\d+@|@\\d+\\.com

要使用它,您可以使用或从 .filtergreplstr_detectstringr

data %>% filter(grepl('^\\d+@|@\\d+\\.com', email))

要获取非垃圾邮件行,请使用 否定条件。!

data %>% filter(!grepl('^\\d+@|@\\d+\\.com', email))

例:

x <- c('[email protected]', '[email protected]', '[email protected]', '[email protected]')
grepl('^\\d+@|@\\d+\\.com', x)
#[1]  TRUE  TRUE  TRUE FALSE

评论

1赞 AdrianHHH 11/3/2023
请注意,这与正则表达式中没有任何内容相同。内部不能删除,因为 之前有一些东西(即 )。(\\d+)@(\\d)@+@\\d+\\.com@\\d+
0赞 Assa Yeroslaviz 11/3/2023
谢谢,这确实很简单,但是有没有办法测试,如果地址开头只有数字?
0赞 DuesserBaest 11/3/2023
如果将正则表达式减少到 那么,它只会过滤 之前的独占数字。^\\d+@@
1赞 Moritz 11/3/2023 #2

这是一个相当简单的解决方案,我认为可能有一种更干净的方法,而无需创建所有这些额外的列:

adress <- c("[email protected]","[email protected]","[email protected]")

adf <- as.data.frame(adress)

adf[c("Before","After")] <- str_split_fixed(adf$adress, '@',2) # this will split the address before @

adf[c("After2","com")] <- str_split_fixed(adf$After,"\\.",2) # this will split the remaining @ 

library(dplyr)
adf <- adf %>% filter(grepl('[a-zA-Z]', Before)) 

adf <- adf %>% filter(grepl('[a-zA-Z]', adf$After2))

adf$adress

[1] "[email protected]"