【问题标题】:How to properly ignore case in regex如何正确忽略正则表达式中的大小写
【发布时间】:2020-06-30 05:10:53
【问题描述】:

这是关于R的。有人可以看看这个:

{library(forcats)
x <- filter(gss_cat, rincome != regex("not applicable", ignore_case = TRUE))}

ignore_case = TRUE 无效。 “不适用”和“不适用”在搜索中看起来仍然不同。

【问题讨论】:

    标签: r regex ignore-case


    【解决方案1】:

    考虑这个例子:

    df <- data.frame(a = c('This is not applicable', 'But this is applicable', 
                           'This is still NOT aPPLicable'))
    

    您需要在stringr 函数之一中使用regex,例如str_detect

    library(dplyr)
    library(stringr)
    
    df %>% filter(str_detect(a, regex('not applicable', 
                  ignore_case = TRUE), negate = TRUE))
    
    #                      a
    #1 But this is applicable
    

    或者在基础 R 中使用 subsetgrepl

    subset(df, !grepl('not applicable', a, ignore.case = TRUE))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多