2

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?

1
  • Edited to clarify that you are using data.table. Please correct if I was mistaken. Commented Oct 12, 2022 at 23:15

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

?`%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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.