【发布时间】:2015-07-28 07:10:11
【问题描述】:
我知道在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))
预期回报:
[1] "De"
实际回报:
[1] "De A-bom, Sint..."
但请注意,正则表达式本身 does work online.
【问题讨论】:
-
字符串
"( |[!\",.:;?})\\]])"中的所有字符都不取决于大小写。 -
@SvenHohenstein 不,但是 df$node 的内容可以。
-
穴居人的解决方案是在应用 strsplit 之前在
sentence上使用tolower。 -
如果我理解了您的问题,与@RomanLuštrik 类似的想法是将句子替换为
gsub(node, node, sentence, ignore.case=TRUE),这将删除相关文本的大小写问题,而不会更改其余部分的大小写句子。 -
我认为您必须添加
perl=TRUE参数。这和tolower接近你想要的结果:strsplit(tolower(sentence), paste0("(?i) ", node, "( |[!\",.:;?})\\]])"),perl=TRUE)
标签: regex r case-sensitive strsplit