Home > OS >  Check if a string contains another string in R when strings contain special characters
Check if a string contains another string in R when strings contain special characters

Time:10-13

In R, using data.table, I was testing if a string contains another string as follows:

library(data.table)
string1 <- "AAAAA"
string2 <- "AAA"
print(string1 %like% string2)

The above returns TRUE. But when string looks like

string1 <- "(A)_"
string2 <- "(A)_"
print(string1 %like% string2)

The above returns FALSE.

Is there any way to fix this other than removing special charaters?

CodePudding user response:

Instead of using the %like% operator, you can use the like function, which has additional arguments.

One of those arguments is fixed, which allows you to ignore regular expressions. So:

like("(A)_", "(A)_", fixed = TRUE)

returns TRUE.

Note that this is identical to how grepl behaves, which is the more common base version of like.

CodePudding user response:

?`%like%` reveals that:

Internally, like is essentially a wrapper around base::grepl

If you treat string2 as a regular expression by escaping any characters that represent special characters in a regular expression the comparison will yield the result you expect (TRUE)

string1 <- "(A)_"
string2 <- "\\(A\\)_"
print(string1 %like% string2)
  • Related