我如何在R中使用带`grep`的反向引用? [英] How can I use back references with `grep` in R?

查看:151
本文介绍了我如何在R中使用带`grep`的反向引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我想找到以a开头的字符串月份名称:

  x < -  c(2011年5月1日,2011年6月30日)
grep(May | ^ June,x,value = TRUE)
[1]2011年5月1日

这很有效,但我真的想隔离这个月份(即May,而不是整个匹配的字符串。



可以使用 gsub 来使用替换参数返回返回引用,但这有两个问题:


  1. 您必须将模式包装在。*(pattern)。*)中,以便替换发生在整个字符串中。

  2. gsub 返回原始字符串,而不是为不匹配的字符串返回NA。这显然不是我想要的:

代码和结果:

  gsub(。*(^ May | ^ June)。,\\ 1,x)
[1]May2011年6月30日

我可能通过做各种附加检查来编写解决方法,但这很快就变得非常混乱



要清楚,理想的结果应该是:

pre $ code > [1]MayNA

有没有简单的方法来实现这个目标?

解决方案

stringr 包中有一个函数完全用于此目的:

$ (2011年5月1日,2011年6月30日,2012年6月),b
$ b

  library(stringr)
x < )
str_extract(x,May | ^ June)
#[1]May不适用六月

regexpr ,但 stringr 通常是字符串处理作为米更容易矿石与基准R函数一致。

I am looking for an elegant way of returning back references using regular expressions in R. Le me explain:

Let's say I want to find strings that start with a month name:

x <- c("May, 1, 2011", "30 June 2011")
grep("May|^June", x, value=TRUE)
[1] "May, 1, 2011"

This works, but I really want to isolate the month (i.e. "May", not the entire matched string.

So, one can use gsub to return the back reference using the substitute parameter. But this has two problems:

  1. You have to wrap the pattern inside ".*(pattern).*)" so that the substitution occurs on the entire string.
  2. Rather than returning NA for non-matched strings, gsub returns the original string. This is clearly not what I desire:

The code and results:

gsub(".*(^May|^June).*", "\\1", x) 
[1] "May"          "30 June 2011"

I could probably code a workaround by doing all kinds of additional checks, but this quickly becomes very messy.

To be crystal clear, the desired results should be:

[1] "May"          NA

Is there an easy way of achieving this?

解决方案

The stringr package has a function exactly for this purpose:

library(stringr)
x <- c("May, 1, 2011", "30 June 2011", "June 2012")
str_extract(x, "May|^June")
# [1] "May"  NA     "June"

It's a fairly thin wrapper around regexpr, but stringr generally makes string handling easier by being more consistent than base R functions.

这篇关于我如何在R中使用带`grep`的反向引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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