【问题标题】:Identifying substring with non-letter characters in string in R data table [duplicate]在R数据表中识别字符串中具有非字母字符的子字符串[重复]
【发布时间】:2021-02-17 13:06:54
【问题描述】:

我正在使用数据表,我想在较长的字符串中用子字符串“..”标记观察结果。看了How to select R data.table rows based on substring match (a la SQL like)我试过了

like("Hi!..", "..")

返回 TRUE 和

like("Hi!..", "Bye")

返回 FALSE。然而,令人惊讶的是,

like("Hi!". "..")

返回真!如果这是一个功能,那是为什么呢?如果我想检查子字符串中的非字母字符,我可以使用什么来代替?

【问题讨论】:

    标签: r


    【解决方案1】:

    您必须转义特殊字符“。”带“\”:

    like("Hi!", "\\.\\.")

    【讨论】:

      【解决方案2】:

      like()的第二个参数是正则表达式,.在正则表达式中有特殊含义;它匹配所有字符。如果要查找.literature,则添加参数fixed = TRUE

      like("Hi!", "..", fixed = TRUE)
      # [1] FALSE
      like("Hi!..", "..", fixed = TRUE)
      # [1] TRUE
      

      【讨论】:

        猜你喜欢
        • 2020-03-30
        • 1970-01-01
        • 2017-07-09
        • 2015-10-05
        • 2021-09-10
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多