简体   繁体   English

使用正则表达式提取R括号中的数字

[英]Extracting number in parenthesis in R using regex

I am trying to extract the 22 from below: 我正在尝试从下面提取22:

"Feb22 19  (22) 100  (Weeklys) "

I have tried the below but with no luck. 我尝试了以下方法,但是没有运气。 Any suggestions? 有什么建议么?

grep("\\(.*\\)", "Feb22 19  (22) 100  (Weeklys) ", value= TRUE

We can try using sub with a capture group: 我们可以尝试将sub与捕获组一起使用:

x <- "Feb22 19  (22) 100  (Weeklys) "
sub(".*\\((\\d+)\\).*", "\\1", x)

[1] "22"

The above pattern can be explained as: 上面的模式可以解释为:

.*     consume anything, up until the last
\(     literal open parenthesis, which is then followed by
(\d+)  one or more digits (which are captured)
\)     followed by a closing parenthesis
.*     followed by anything

The replacement is \\\\1 , which is the number captured in the pattern. 替换为\\\\1 ,这是模式中捕获的数字。 Note that should the input not contain a number in parentheses, the above call to sub would actually return the original input string. 请注意,如果输入的括号中包含数字,则对sub的上述调用实际上将返回原始输入字符串。 If you don't like this behavior, then you will have to do more work. 如果您不喜欢这种行为,那么您将不得不做更多的工作。

We can also use: 我们还可以使用:

    string<-"Feb22 19 (22) 100 (Weeklys) "
    unlist(stringr::str_extract_all(string,"\\d{1,}(?=\\))"))
    #[1] "22"

I was recently advised to use simplify although I find unlist 's output better. 最近,我建议使用simplify虽然我找到unlist的输出更好。

Using stringr::str_extract_all(string,"\\\\d{1,}(?=\\\\))",simplify=TRUE) 使用stringr::str_extract_all(string,"\\\\d{1,}(?=\\\\))",simplify=TRUE)

    [,1]
[1,] "22"

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

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