简体   繁体   English

R中的agrep - 在字符串中查找* all *匹配(全局标志)

[英]agrep in R - find *all* matches in a string (global flag)

I've got a string: 我有一个字符串:

string <- "I do not like green eggs and ham!"

and a pattern 和一个模式

pattern <- "(egs|ham)"

I want to know how many times pattern matches string with fuzzy matching (agrep) . 我想知道pattern匹配string与模糊匹配(agrep)的次数

gregexpr will do this for normal matching - I just want to know if there's a corresponding garegexpr in R or a way to emulate it without being too performance-heavy. gregexpr将执行此操作以进行正常匹配 - 我只是想知道R中是否存在相应的garegexpr ,或者是一种模拟它的方式而不会过于高性能。

( aregexec will only return the index for the first match, "eggs", and skip "ham"). aregexec只返回第一个匹配的索引,“eggs”,并跳过“ham”)。

You didn't specify that you need base R, so I'll happily suggest using the str_count(string, pattern) function from the "stringr" package by Hadley Wickham. 你没有指定你需要基数R,所以我很乐意建议使用Hadley Wickham的“stringr”包中的str_count(string,pattern)函数。

library(stringr)
string <- "I do not like green eggs and ham!"
pattern <- "(egs|ham)"
str_count(string, pattern)
[1] 1

stringr really is a great R package. stringr真的是一个很棒的R包。 Full of all sorts of string usefulness. 充满各种字符串的用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM