正则表达式在括号之间挑选一些文本 [英] regex to pickout some text between parenthesis

查看:58
本文介绍了正则表达式在括号之间挑选一些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
提取R中所有括号内的信息(正则表达式) >

我有一个字符串

df

Peoplesoft(id-1290)

例如,我喜欢捕获括号之间的字符.我喜欢从上面的例子中得到 id-1290.

I like to capture characters between the parentesis, for example. I like to get id-1290 from the above example.

我用过这个:

x <- regexpr("\\((.*)\\)", df) 

这给了我像

[1] 10

是否有一种简单的方法可以在 R 中使用正则表达式在括号之间抓取文本?

Is there an easy way to grab text between parentesis using regex in R?

推荐答案

为此我更喜欢使用 gsub():

gsub(".*\\((.*)\\).*", "\\1", df)
[1] "id-1290"

<小时>

正则表达式的工作原理如下:


The regex works like this:

  • 在括号内查找文本 - 不是您真正的括号,而是我额外的一组括号,即 (.*)
  • 将此作为反向引用返回,\\1

换句话说,用反向引用替换字符串中的所有文本

In other words, substitute all text in the string with the back reference

如果您想使用 regexp 而不是 gsub,请执行以下操作:

If you want to use regexp rather than gsub, then do this:

x <- regexpr("\\((.*)\\)", df)
x

[1] 11
attr(,"match.length")
[1] 9
attr(,"useBytes")
[1] TRUE

这将返回值 11,即找到的表达式的起始位置.并注意表示匹配的字符数的属性 match.length.

This returns a value of 11, i.e. the starting position of the found expression. And note the attribute match.length that indicates how many characters were matched.

您可以使用 attr 提取:

attr(x, "match.length")
[1] 9

然后使用substring提取字符:

substring(df, x+1, x+attr(x, "match.length")-2)
[1] "id-1290"

这篇关于正则表达式在括号之间挑选一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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