Reputation: 2858
I have a string that looks like this
Some text (group1) some more text (group2) some more text (groupN)
Can somebody help me to write a regular expression that matches groupN? That group is always at the end of the text.
I tried this expression
\(.+\)
But, it matches from the opening parenthesis of group1 to the closing parenthesis of groupN. I only need to match groupN.
Thanks for your help.
Upvotes: 1
Views: 120
Reputation: 4795
If the final parenthesis might have text appearing after it, as in:
some text (first) and (last) more text
You can use a lookahead to check there are no more parenthesis in the string after the match:
\([^)]*\)(?=[^(]*$)
If not then just match a $
as falsetru correctly suggests.
Upvotes: 2
Reputation: 369494
Use following regular expression. It matches only at the end of the input string. ($
matches the end of the input string or end of line according to regular expression engines.)
\([^()]+\)$
Javascript example:
> 'Some text (group1) some more text (group2) some more text (groupN)'.match(/\([^()]+\)$/)
["(groupN)"]
Upvotes: 2