Home > database >  regex golang last match of a pattern inside curly braces
regex golang last match of a pattern inside curly braces

Time:08-13

I have the following string, and I want to match the contents of the last curly brace (inclusive), i.e. the output should be {ahhh}.

abc {popo}/popo/{ahhh}

Golang does not support negative lookahead, and I have tried the following patterns but it has not worked

{. ?}$
{. ?}([^/])

Any help would be much appreciated. Thank you.

CodePudding user response:

You could match from an opening till closing curly at the end of the string:

\{[^{}]*}$

Regex demo

Or you could match the whole line, and then capture the last occurrence of the curly's followed by matching any char except / till the end of the string.

.*(\{[^{}]*})[^/]*$

Regex demo

  • Related