stringr,str_extract:如何进行正面回顾? [英] stringr, str_extract: how to do positive lookbehind?

查看:33
本文介绍了stringr,str_extract:如何进行正面回顾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题.我只需要使用正则表达式正向后视来捕获一些字符串,但我看不到这样做的方法.

Very simple problem. I just need to capture some strings using a regex positive lookbehind, but I don't see a way to do it.

这是一个例子,假设我有一些字符串:

Here's an example, suppose I have some strings:

library(stringr)
myStrings <- c("MFG: acme", "something else", "MFG: initech")

我想提取前缀为MFG:"的单词

I want to extract the words which are prefixed with "MFG:"

> result_1  <- str_extract(myStrings,"MFG\\s*:\\s*\\w+")
>
> result_1
[1] "MFG: acme"    NA             "MFG: initech"

几乎做到了,但我不想包括MFG:"部分,所以这就是正面回顾"的用途:

That almost does it, but I don't want to include the "MFG:" part, so that's what a "positive lookbehind" is for:

> result_2  <- str_extract(myStrings,"(?<=MFG\\s*:\\s*)\\w+")
Error in stri_extract_first_regex(string, pattern, opts_regex = attr(pattern,  : 
  Look-Behind pattern matches must have a bounded maximum length. (U_REGEX_LOOK_BEHIND_LIMIT)
> 

它抱怨需要一个有界最大长度",但我不知道在哪里指定.我如何使积极的后视工作?确切地说,我可以在哪里指定这个有界最大长度"?

It is complaining about needing a "bounded maximum length", but I don't see where to specify that. How do I make positive-lookbehind work? Where, exactly, can I specify this "bounded maximum length"?

推荐答案

你需要使用 str_match 因为lookbehind"的模式是一个文字,你只是不知道空格的数量:

You need to use str_match since the pattern for "lookbehind" is a literal, and you just do not know the number of whitespaces:

> result_1  <- str_match(myStrings,"MFG\\s*:\\s*(\\w+)")
> result_1[,2]
##[1] "acme"    NA        "initech"

您需要的结果将在第二列中.

The results you need will be in the second column.

注意 str_extract 不能在此处使用,因为该函数会丢弃捕获的值.

Note the str_extract cannot be used here since that function drops the captured values.

还有一个好处:后视不是无限宽度,而是 ICU 正则表达式中的约束宽度.因此,这也将起作用:

And a bonus: the lookbehind is not infinite-width, but it is constrained-width in ICU regex. So, this will also work:

> result_1  <- str_extract(myStrings,"(?<=MFG\\s{0,100}:\\s{0,100})\\w+")
> result_1
[1] "acme"    NA        "initech"

这篇关于stringr,str_extract:如何进行正面回顾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆