括号之间的返回数字

时间:2013-09-02 17:09:37

标签: r

我是一名没有正则表达式培训的休闲程序员,所以我正在努力解决这个问题。我在这里找到了一个例子:

Is there an efficient way of returning a sub-section of a text string

其中gsub用于提取两个时期之间的数字。我需要做同样的事情,但是我正在寻找方括号之间的数字,就像在tmpstr1中一样。有人可以帮我理解gsub示例中的正则表达式,以便我可以修改它并让它工作吗?基本上,如何用英语读取正则表达式部分,这样才有意义?我只是不明白。

由于

set.seed(1)
n <- 50
let1 <- LETTERS[runif(n, min=1, max=26)]
num <- round(runif(100, min=1, max=100))
let2 <- c(LETTERS[runif(n, min=1, max=26)], LETTERS[runif(n, min=1, max=26)])
tmpstr <- paste(let1, num, let2, sep=".")
tmpstr

gsub('.*[.](.*)[.].*','\\1',tmpstr)

tmpstr1 <- paste0(let1, "[", num, "]", let2)
tmpstr1

2 个答案:

答案 0 :(得分:2)

在发布问题后,我发现了一个似乎有效的例子:

gsub('.+\\[([0-9]+)\\].+?$', '\\1', tmpstr1)

答案 1 :(得分:0)

对于那些想要学习正则表达式的人来说,LGTrader的答案更好(我的投票在那里),但这适用于那些不想学习正则表达式的人:

library(qdap)
x <- bracketXtract(tmpstr1, "square")
unname(unlist(x))

## > unname(unlist(bracketXtract(tmpstr1, "square")))
##   [1] "48" "86" "44" "25" "8"  "11" "32" "52" "67" "41" "91" "30" "46" "34" "65"
##  [16] "27" "48" "77" "9"  "88" "35" "84" "35" "34" "48" "89" "87" "40" "78" "96"
##  [31] "44" "72" "41" "33" "76" "21" "71" "13" "25" "15" "25" "7"  "65" "88" "78"
##  [46] "80" "46" "42" "81" "61" "66" "36" "28" "99" "64" "22" "14" "48" "92" "60"
##  [61] "98" "73" "36" "44" "16" "2"  "72" "11" "45" "64" "99" "50" "49" "18" "76"
##  [76] "46" "52" "22" "24" "60" "58" "9"  "5"  "65" "93" "60" "57" "53" "99" "51"
##  [91] "69" "61" "25" "27" "73" "46" "18" "75" "11" "87"