简体   繁体   中英

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), ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM