I have the below string:
test 13 (8) end 12 (14:48 IN 1ST)
I need the output to be:
14:48 IN 1ST
or everything inside parentheses towards the end of the string.
I don't need, 8 which is inside first set of parentheses. There can be multiple sets of parentheses in the string. I only need to consider everything inside the last set of parentheses of input string.
This regex will do:
.+\((.+?)\)$
Escape the parentheses, make the + non-greedy with ?, and make sure it's at the end of the line.
If there may be characters after it, try this instead:
Which basically makes sure only the second parentheses will match. I would still prefer the first.
I would go with the following regex:
I wouldn't use a regex for this, it's unnecessary.
Use strrpos and substr to extract the string that you need. It's simple, straightforward, and achieves the desired output.
It works by finding the last '(' in the string, and removing one character from the end of the string.
Demo
Edit: I should also note that my solution will work for all of the following cases:
Final edit: Again, I cannot emphasis enough that using a regex for this is unnecessary. Here's an example showing that string manipulation is 3x - 7x faster than using a regex.
As per MetaEd's comments, my example / code can easily be modified to ignore text after the last parenthesis.
STILL faster than a regex.
Regex Explanation
preg_match
Accept Empty preg_match_all
Don't Accept Empty preg_match_all
\(.*\)
will match the first and last parens. To prevent that, begin with.*
which will greedily consume everything up to the final open paren. Then put a capture group around what you want to output, and you have:The easiest thing would be to split the string on ')' and then just grab everything from the last item in the resulting array up till '('... I know it's not strictly regex but it's close enough.
[edit] (to make sure people who love to down-vote things before actually understanding what's being said... (like JV), can have actual code to consider)...
This will produce an array with two elements... "test 13 (8" and " end 12 (14:48 IN 1ST"
Notice that no matter how many (xyz) you have in there you will end up with the last one in the last array item.
Then you just look through that last item for a '(' and if it's there grab everything behind it.
I suspect this will work faster than a straight regex approach, but I haven't tested, so can't guarantee that... regardless it does work. [/edit]