Write regular expressions [closed]

I would suggest something like this:

/\) (?!.*\))(\S+)/

rubular demo

Or if you don’t want to have capture groups, but potentially slower:

/(?<=\) )(?!.*\))\S+/

rubular demo

(?!.*\)) is a negative lookahead. If what’s inside matches, then the whole match will fail. So, if .*\) matches, then the match fails, in other terms, it prevents a match if there’s a ) after that position in the match.

In the second regex, (?<=\) ) is a positive lookbehind, where it ensures that there’s a ) before the match starts.

Leave a Comment