简体   繁体   English

grepl在R中没有正确搜索

[英]grepl not searching correctly in R

I want to search ".com" in a vector, but grepl isn't working out for me.我想在向量中搜索“.com”,但 grepl 不适合我。 Anyone know why?有谁知道为什么? I am doing the following我正在做以下事情

vector <- c("fdsfds.com","fdsfcom")
grepl(".com",vector)

This returns这返回

[1] TRUE TRUE

I want it to strictly refer to "fdsfds.com"我希望它严格参考“fdsfds.com”

As @user20650 said in the comments above, use grepl("\\\\.com",vector) .正如@user20650 在上面的评论中所说,使用grepl("\\\\.com",vector) the dot ( . ) is a special character in regular expressions that matches any character, so it's matching the second "f" in "fdsfcom".点 ( . ) 是正则表达式中匹配任何字符的特殊字符,因此它匹配“fdsfcom”中的第二个“f”。 The "\\\\" before the .之前的"\\\\" . "escapes" the dot so it's treated literally. “转义”点,因此按字面意思处理。 Alternatively, you could use grepl(".com",vector, fixed = TRUE) , which searches literally, not using regular expressions.或者,您可以使用grepl(".com",vector, fixed = TRUE) ,它按字面搜索,而不是使用正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM