gpt4 book ai didi

r - 使用 str_detect 检测字符串中的模式

转载 作者:行者123 更新时间:2023-12-01 11:42:51 29 4
gpt4 key购买 nike

我正在尝试使用 str_detect 检测字符串是否包含特定模式。我的图案是一系列“....” - 确切的点数未知。我正在尝试使用 str_detect 如下....

但是,在这种特殊情况下,str_detect 返回 TRUE。想知道我哪里做错了,str_detect 是否是正确的函数?希望这里有人可以提供帮助?

library(stringr)
dot_pat="\\.........................";
str="The primary.objective is of the study."
str_detect(str,dot_pat)

这将返回 TRUE。我期待 FALSE 因为 str 中的点不遵循模式。

提前致谢,西马克

最佳答案

您的模式表示:一个点 (\\.) 后跟 24 个符号。所以这匹配:“.objective is of the stu”。

如果您想检测 10 个点符号,请使用如下模式:dot_pat="\.{10}"

str_detect("The primary.objective is of the study.", "\\.{10}")
str_detect("hello..........world", "\\.{10}")

关于r - 使用 str_detect 检测字符串中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17804917/

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