简体   繁体   中英

R regular expression: find the last but one match

I need a regular expression script (in R language) which finds the last but one match.

Here is an example:

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

The result has to be: 7 and 16 , because in the first case the last but one '(' is in a 7th position (from left), and in the second case the last but one '(' in in the 16th position (from left).

I've found a regular expression which can find the last match, but I could not transform it in the right way:

\\([^\\(]*$

Thanks for any help!

To match a chunk of text beginning with the last but one ( , you may use

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

Details :

  • (\\\\([^(]*){2} - 2 sequences of:
    • \\( - a literal (
    • [^(]* - zero or more chars other than (
  • $ - end of string.

R test:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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