How to make% like% operator case insensitive

Is there a way to turn the %like% operator into a datatable package datatable insensitive? So, for example, 'hello' %like% 'HELlo' will match.

+5
source share
1 answer

Without relying on the definition in data.table :

 `%like%` <- function (x, pattern) { stringi::stri_detect_regex(x, pattern, case_insensitive=TRUE) } 

data.table defines it as:

 function (vector, pattern) { if (is.factor(vector)) { as.integer(vector) %in% grep(pattern, levels(vector)) } else { grepl(pattern, vector) } } 

If you want, you can cover the case of factor , but this is not a very complicated function. There is no "magic" in it.

I use stringi like this (for my work) much more reliable than the built-in string operators, and provides much more power under the hood.

You can also define it as:

 `%like%` <- function (x, pattern) { grepl(pattern, x, ignore.case=TRUE) } 

(again, ignoring the factor case) if you want. You lose the vectorized pattern doing this, tho.

Make the name %likeic% (for example, ignore case) if you do not want to extrude the definition for data.table %like% .

+6
source

Source: https://habr.com/ru/post/1262171/


All Articles