theamateurdataanalyst
theamateurdataanalyst

Reputation: 2834

grepl not searching correctly in R

I want to search ".com" in a vector, but grepl isn't working out for me. 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"

Upvotes: 1

Views: 3499

Answers (1)

andyteucher
andyteucher

Reputation: 1453

As @user20650 said in the comments above, use 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". 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.

Upvotes: 6

Related Questions