【问题标题】:Matching a single character within a string in R [duplicate]匹配R中字符串中的单个字符[重复]
【发布时间】:2019-04-17 08:24:37
【问题描述】:

我断言如果有一个“。”在 R 中的字符串中,但 grepl 总是返回错误。谁能解释我哪里出错了?

这是我的代码:

grepl("testtxt",".")
[1] FALSE
grepl("test.txt",".")
[1] FALSE

【问题讨论】:

    标签: r grepl


    【解决方案1】:

    我们需要fixed = TRUE

    grepl("test.txt", pattern = ".", fixed = TRUE)
    #[1] TRUE
    

    注意:pattern 是grep/grepl 的第一个参数如果我们以不同的顺序指定它,请确保为参数命名

    或转义(\\.).,因为. 是匹配任何字符的元字符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多