从agrep提取子字符串匹配

我的目标是确定给定的OutlinedButton中是否有一个 <style name="Outlined.MaterialComponents.Button.TextButton.snackbar" parent="@style/Widget.MaterialComponents.Button.OutlinedButton"> <item name="strokeColor">@color/...</item> <item name="strokeWidth">1dp</item> <item name="android:textColor">@color/...</item> </style> 字符串,但是我想允许输入错误/小的派生并提取“引起”匹配的子字符串(使用以便进行进一步的文本分析。

示例:

text

所需的输出:

我想将target作为输出,因为ist非常接近目标(左行距1)。接下来,我想使用target <- "target string" text <- "the target strlng: Butter. this text i dont want to extract." 提取单词target strlng(我已经介绍了这一部分,我只是添加了它以具有详细的说明)。

我尝试过的事情:

使用adist无效,因为它比较两个字符串,而不是子字符串。

接下来,我看了一下target strlng,它似乎非常接近。我可以找到找到目标的输出,但是找不到“导致”匹配的Butter

我尝试使用agrep,但是它似乎可以在Array Level上工作。我认为我不可能切换到数组类型,因为我不能按空格分割(我的目标字符串可能有空格,...)。

substring
java21710397 回答:从agrep提取子字符串匹配

使用aregexec,类似于使用regexpr/regmatches(或gregexpr)进行精确匹配提取。

m <- aregexec('string','text strlng wrong')
regmatches('text strlng wrong',m)
#[[1]]
#[1] "strlng"

这可以包装在使用aregexecregmatches的参数的函数中。请注意,在后一种情况下,函数参数invert在点参数...之后之后,因此它必须是命名参数。

aregextract <- function(pattern,text,...,invert = FALSE){
  m <- aregexec(pattern,...)
  regmatches(text,m,invert = invert)
}

aregextract(target,text)
#[[1]]
#[1] "target strlng"

aregextract(target,invert = TRUE)
#[[1]]
#[1] "the "                                       
#[2] ": Butter. this text i dont want to extract."
本文链接:https://www.f2er.com/3080996.html

大家都在问