1

I am seeking to extract every time an open parenthesis + any number of dots + close parenthesis happens within my string.

The following is my R code:

pos_test <- "..((((............))))))))))....((........))(((....)))..."

pos_test_matrix <- unlist(strsplit(pos_test, ""))

grep(pos_test_matrix, pattern = "[(]+[.]+[)]")

Please advise, the grep returns integer(0)

What I'd like is the following:

Output from http://regexr.com/

oguz ismail
  • 1
  • 16
  • 47
  • 69
laemtao
  • 147
  • 11

1 Answers1

1

Using the stringr package:

x <- "..((((............))))))))))....((........))(((....)))..."

library(stringr)

str_extract_all(x, "\\(+\\.*\\)+")
[[1]]
[1] "((((............))))))))))"    "((........))"     "(((....)))"

Or in base R:

mx = gregexpr("\\(+\\.*\\)+", x)

sapply(1:length(mx[[1]]), function(i) {

  substr(x, mx[[1]][i], mx[[1]][i] + attr(mx[[1]], "match.length")[i] - 1)

})
[1] "((((............))))))))))"    "((........))"     "(((....)))"
eipi10
  • 91,525
  • 24
  • 209
  • 285