正则表达式 – 使用grepl从模式列表中查找匹配的模式
发布时间:2020-12-14 06:02:16 所属栏目:百科 来源:网络整理
导读:我使用grepl检查一个字符串是否包含一组模式中的任何模式(我使用’|’来分隔模式).反向搜索没有帮助.如何识别匹配的模式集? 附加信息:这可以通过编写循环来解决,但是由于我的设置具有所以非常耗时. 100,000字符串.可以优化吗? 例如:让字符串为 - “Hello
我使用grepl检查一个字符串是否包含一组模式中的任何模式(我使用’|’来分隔模式).反向搜索没有帮助.如何识别匹配的模式集?
附加信息:这可以通过编写循环来解决,但是由于我的设置具有>所以非常耗时. 100,000字符串.可以优化吗? 例如:让字符串为< - “Hello” pattern <- c("ll","lo","hl") pattern1 <- paste(pattern,collapse="|") # "ll|lo|hl" grepl(a,pattern=pattern1) # returns TRUE grepl(pattern,pattern=a) # returns FALSE 'n' times - n is 3 here 解决方法
您正在从包stringr中寻找str_detect:
library(stringr) str_detect(a,pattern) #[1] TRUE TRUE FALSE 如果您有多个字符串,如a = c(‘hello’,’hola’,’plouf’),您可以执行以下操作: lapply(a,function(u) pattern[str_detect(u,pattern)]) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |