2016-05-19 33 views
1

我正在尋求提取每一次左括號+任意數量的點+關閉括號發生在我的字符串內。grep與R的RNA括號表示

以下是我的R代碼裏面:

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

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

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

請指教,grep的返回integer(0)

我想是這樣的:

Output from http://regexr.com/

+2

'gregexpr( 「\\(\\ * \\)。」,post_test)'? – eipi10

+0

我添加了一個截圖來解釋我正在尋找的輸出 – laemtao

回答

1

使用stringr包:

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

library(stringr) 

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

或者在基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] "((((............))))))))))" "((........))"  "(((....)))"