看起来grep在返回匹配的方式中是“贪婪”的。假设我有以下数据:R grep和精确匹配
Sources <- c(
"Coal burning plant",
"General plant",
"coalescent plantation",
"Charcoal burning plant"
)
Registry <- seq(from = 1100, to = 1103, by = 1)
df <- data.frame(Registry, Sources)
如果我执行grep("(?=.*[Pp]lant)(?=.*[Cc]oal)", df$Sources, perl = TRUE, value = TRUE)
,它返回
"Coal burning plant"
"coalescent plantation"
"Charcoal burning plant"
不过,我只想要回精确匹配,即只有在“煤”和“植物”发生。我不想要“聚结剂”,“种植园”等。因此,对于这个,我只是想看看"Coal burning plant"
只需使用''==然后,如果你想完全匹配:'$ DF来源[DF $来源== “燃煤电厂”]' – thelatemail
@thelatemail这将在这方面的工作实例。但想象一下,如果我还有其他条目,如“煤电蒸汽机厂”,“柴油机厂”等,我也想回复这些。 – sedeh
这真的不是同一个问题。如果你在这个问题上改变people.jpg来表示人们,然后做一个你在寻找人的grep,它会以“人民”的身份回归事件。您可以运行下面看看我说的话:'X < - C( “imageUploaded,人民更,逗号分隔,东西”, “imageUploaded”, “people.jpg” ) xmatches < - 相交( ) x [xmatches]' – sedeh