preg_match - pattern - last word within parentheses
I'm trying to use a regex to get the last word within the parentheses if there is more than 1 word inside. In all cases will be Baz
! KEYWORD (\Foo \Bar \Ban \Baz) "/" "Hello"
! KEYWORD (\开发者_如何学GoFoo \Bar \Baz) "/" "Hello"
! KEYWORD (\Foo \Baz) "/" "Hello"
Thanks
One possibility would be
.*\W(\w+)\s*\)
Explanation
.* #1 runs to the end of the string
\W #2 backtracks to a non-"word character"
(\w+) #3 captures all following "word characters" into group 1
\s* #4 optional white space
\) #5 mandatory literal closing paren
This regex works because it actively uses backtracking to find "the last occurrence of something" in a string (i.e. step #2 is repeated until the rest of the expression can match - or the entire expression fails).
In this case something is a sequence of word characters preceded by a non-word character and followed by a closing paren.
精彩评论