我知道在 grep 中你可以简单地使用 ignore.case = TRUE
。但是, strsplit 呢?您可以将正则表达式作为第二个参数传递,但我不确定如何使此正则表达式不区分大小写。
目前,这就是我的 strsplit 的样子,但我想让搜索不区分大小写。我该怎么做?
strsplit(df$sentence, paste0(" ", df$node, "( |[!\",.:;?})\\]])"))
例子:
sentence <- "De A-bom, Sint...";
node <- "a-bom"
contexts <- strsplit(sentence, paste0("(?i) ", node, "( |[!\",.:;?})\\]])"))
(leftContext <- sapply(contexts, `[`, 1))
预期 yield :
[1] "De"
实际返回:
[1] "De A-bom, Sint..."
但是请注意,正则表达式本身 does work online 。
最佳答案
"(?i)"模式修饰符确实使基于 PCRE 的正则表达式不区分大小写。
您的示例的问题不在于大小写,而在于分组表达式中。将 perl=TRUE
用于您预期的转义行为。
sentence <- "De A-bom, Sint...";
node <- "a-bom"
contexts <- strsplit(sentence, paste0("(?i) ", node,
"( |[!\",.:;?})\\]])"),perl=TRUE)
(leftContext <- sapply(contexts, `[`, 1))
产生预期的
[1] "De"
关于regex - 在 R 中忽略 strsplit 中的大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31669602/