R中的stringr:str_replace_all不遵守'ignore_case = TRUE'

问题描述 投票:1回答:1

这是str_replace_all中的错误还是真的误解了?

library(tidyverse)
packageVersion("tidyverse")
# [1] ‘1.2.1’
R.version.string
# [1] "R version 3.5.2 (2018-12-20)"

fruits <- c("one apple", "two pears", "three bananas")

这按预期工作:

fruits %>%
  str_replace_all(c("on\\w+" = "1", "two" = "2", "three" = "3"))
# [1] "1 apple"   "2 pears"   "3 bananas"

也是这样:

fruits %>% 
  str_replace_all(c(regex("on\\w+", ignore_case = TRUE), "two", "three"),
                  c("1", "2", "3"))
# [1] "1 apple"   "2 pears"   "3 bananas"

但是当我尝试使其不区分大小写时,它不使用ignore_case

fruits %>% 
  str_replace_all(c(regex("ON\\w+", ignore_case = TRUE), "two", "three"),
                  c("1", "2", "3"))
# [1] "one apple" "2 pears"   "3 bananas"

问题似乎出在str_replace_all,而不是regex

fruits %>% 
  str_detect(regex("ON\\w+", ignore_case = TRUE))
# [1]  TRUE FALSE FALSE

出于我的目的,我有一种解决方法,但是-有什么见解吗?

r regex str-replace stringr
1个回答
1
投票

我们可以使用(?i)

© www.soinside.com 2019 - 2024. All rights reserved.