darkvalance
darkvalance

Reputation: 430

regex golang last match of a pattern inside curly braces

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.

Upvotes: 0

Views: 663

Answers (1)

The fourth bird
The fourth bird

Reputation: 163277

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

Upvotes: 2

Related Questions