gpt4 book ai didi

R - 在以破折号结尾的 'words' 上使用字边界时,为什么 str_detect 返回与 grepl 不同的结果

转载 作者:行者123 更新时间:2023-12-02 11:09:35 25 4
gpt4 key购买 nike

str_detect 的帮助页面指出“相当于 grepl(pattern, x)”,但是:

str_detect("ALL-", str_c("\\b", "ALL-", "\\b"))
[1] FALSE

同时

grepl(str_c("\\b", "ALL-", "\\b"), "ALL-")
[1] TRUE

我想其中一个没有按预期工作?或者我错过了什么?

最佳答案

当您将参数 perl = TRUE 添加到 grepl() 时,它会给出相同的结果:

> grepl(str_c("\\b", "ALL-", "\\b"), "ALL-")
[1] TRUE
> grepl(str_c("\\b", "ALL-", "\\b"), "ALL-", perl = T)
[1] FALSE

此参数意味着 grepl() 将使用 Perl 兼容的正则表达式。

?grep中有这个警告,可能与之相关?

The POSIX 1003.2 mode of gsub and gregexpr does not work correctly with repeated word-boundaries (e.g., pattern = "\b"). Use perl = TRUE for such matches (but that may not work as expected with non-ASCII inputs, as the meaning of ‘word’ is system-dependent).

关于R - 在以破折号结尾的 'words' 上使用字边界时,为什么 str_detect 返回与 grepl 不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55509660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com