Reputation: 145
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!
Upvotes: 3
Views: 460
Reputation: 627536
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
Upvotes: 6