带有重叠子串的 str

编程入门 行业动态 更新时间:2024-10-10 15:24:23
本文介绍了带有重叠子串的 str_count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试计算字符向量中子字符串的出现次数.例如:

I am trying to count the number of appearances of a substring within a character vector. For example:

lookin<-c("babababa", "bellow", "ra;baba") searchfor<-"aba" str_count(lookin, searchfor)

返回:2 0 1

但是,我希望它返回 '3 0 1' 但它没有在第一项中的中间 'aba' 上找到,因为它在第一个实例中被部分使用(我认为).

However, I want it to return '3 0 1' but it isn't picking up on the middle 'aba' in the first item since it is partially used in the first instance (I think).

我发现了这个问题无法弄清楚如何将其用于具有多个项目的向量.

I found this question but couldn't figure out how to use that with a vector having multiple items.

推荐答案

尝试:

str_count(lookin, paste0("(?=",searchfor,")")) [1] 3 0 1

正如您的链接中所回答的那样,使用前瞻来匹配所有实例.

Which, as answered in your link, uses lookahead to match all instances.

更多推荐

带有重叠子串的 str

本文发布于:2023-11-30 07:52:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:str

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!