匹配R中字符串中的单个字符

Matching a single character within a string in R

我断言是否有“.”在 R 的字符串中,但 grepl 总是返回 false。谁能解释我哪里出错了?

这是我的代码:

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

我们需要 fixed = TRUE

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

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

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