Ignore case in strsplit in R

I am aware that in grep you can just use ignore.case = TRUE

. However, what about strsplit ? You can pass a regex as the second argument, but I'm not sure how to make this case a regex insensitive.

It currently looks like my strsplit, but I want to make the search case insensitive. How should I do it?

strsplit(df$sentence, paste0(" ", df$node, "( |[!\",.:;?})\\]])"))

      

Example:

sentence <- "De A-bom, Sint..."; 
node <- "a-bom"

contexts <- strsplit(sentence, paste0("(?i) ", node, "( |[!\",.:;?})\\]])"))
(leftContext <- sapply(contexts, `[`, 1))

      

Expected income:

[1] "De"

      

Actual return:

[1] "De A-bom, Sint..."

      

Note that the regex itself works on the web .

+3


source to share


1 answer


The "(? I)" modifier makes PCRE-dependent regular expressions case insensitive.

The problem with your example is not the case, but the grouping expression. Use perl=TRUE

for expected escaping behavior.

sentence <- "De A-bom, Sint..."; 
node <- "a-bom"

contexts <- strsplit(sentence, paste0("(?i) ", node, 
    "( |[!\",.:;?})\\]])"),perl=TRUE)
(leftContext <- sapply(contexts, `[`, 1))

      



Produces the expected

[1] "De"

      

0


source







All Articles