我不明白为什么
grepl("see*", "file SEC", ignore.case = TRUE)
返回TRUE
?
我试图找到所有以
see
开头的单词,例如 See
、seeing
、seen
等,并将其删除。
“file SEC”上面的字符串没有这样的单词,但返回了TRUE
。
使用单词边界 (
\\b
)
模式
"see*"
检查“se”后跟任意数量的 "e"
(e*
)(包括零),因此“SE”匹配。
我相信你可能想研究这样的东西,没有“*”
grepl("^see", "file SEC", ignore.case = TRUE)
FALSE
除了“^”符号之外,您还可以包含单词边界
\\b
,这样您就可以检测以模式开头的单词,但在多单词字符内排除那些不以该模式开头的单词:
grepl("\\bSee", c("file SEC", "See", "seeing", "seen", "he was seen", "He did not forsee the event"), ignore.case = TRUE)
[1] FALSE TRUE TRUE TRUE TRUE FALSE
尝试 grepl("^see", "文件 SEC") “^see”表示“所有以 see 开头的字符串”