【问题标题】:Exact match on word in middle of string in R [duplicate]R中字符串中间的单词完全匹配[重复]
【发布时间】:2021-08-02 10:45:53
【问题描述】:

我提到了这个问题 (How to filter Exact match string using dplyr),但我的问题略有不同,因为这个词不是开头,而是可以出现在字符串中的任何位置。我希望 TRUE 只返回第一个而不是第二个和第三个

library(stringr)

vec <- c("this should be selected", "thisus should not be selected","not selected thisis too")
str_detect(vec,"this")

电流输出

TRUE TRUE TRUE

预期输出

TRUE FALSE FALSE

【问题讨论】:

    标签: r dplyr tidyverse stringr


    【解决方案1】:

    使用单词边界 (\\b)

    stringr::str_detect(vec,"\\bthis\\b")
    #[1]  TRUE FALSE FALSE
    

    在基础 R 中:

    grepl('\\bthis\\b', vec)
    

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      相关资源
      最近更新 更多