-1

I am using data table and I want to mark out observations with a substring ".." in a longer string. After looking at How to select R data.table rows based on substring match (a la SQL like) I tried

like("Hi!..", "..")

which returns TRUE and

like("Hi!..", "Bye")

returns FALSE. However, surprisingly,

like("Hi!". "..")

returns TRUE! If this is a feature, why is that? And what can I use instead if I want to check a substring for non-letter characters?

2 Answers2

0

You have to escape the special character "." with "\":

like("Hi!", "\\.\\.")

ha-pu
  • 581
  • 7
  • 19
0

The second argument to like() is a regular expression and . has a special meaning in regex; it matches all characters. If you want to look for . literary, then add the argument fixed = TRUE.

like("Hi!", "..", fixed = TRUE)
# [1] FALSE
like("Hi!..", "..", fixed = TRUE)
# [1] TRUE
s_baldur
  • 29,441
  • 4
  • 36
  • 69