簡體   English   中英

R正則表達式:找到最后一個匹配

[英]R regular expression: find the last but one match

我需要一個正則表達式腳本(在R語言中),它找到最后一個匹配。

這是一個例子:

input = c("(test1(test2(test3","(((((othertest1(othertest2(othertest3")
regexpr('the_right_regular_expression_here_which_can_finds_the_last_but_one_'(' ', input)

結果必須是: 716 ,因為在第一種情況下,最后一個'('在第7個位置(從左),在第二個案例中最后但是一個'('在第16個位置)從左)。

我找到了一個可以找到最后一個匹配的正則表達式,但我無法以正確的方式對其進行轉換:

\\([^\\(]*$

謝謝你的幫助!

要匹配從最后一個開始的文本塊(您可以使用

"(\\([^(]*){2}$"

細節

  • (\\\\([^(]*){2} - 2個序列:
    • \\( - 文字(
    • [^(]* - 除零以外的零個或多個字符(
  • $ - 結束字符串。

R測試:

> input = c("(test1(test2(test3","(((((othertest1(othertest2(othertest3")
> regexpr("(\\([^(]*){2}$", input)
[1]  7 16
attr(,"match.length")
[1] 12 22
attr(,"useBytes")
[1] TRUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM