简体   繁体   中英

Regex match all inside brackets inside brackets?

I want to find the following value

PROCEDURE test {
        test { }
}

from:

test {
    PROCEDURE test {
        test { }
    }
}

My current Regex is:

PROCEDURE.*?{.*?(\}){2}

But it does not match. Does anyone have any idea how I can accomplish this?

You would need to match pairs of { and } between the first { and the last } .

You can try out this regex: -

PROCEDURE[^{]*[{](?:[^{]*[{][^}]*[}])*[^}]*[}]

I have enclosed curly braces inside character class, so that you don't need to escape them, also with [^{]* , you won't need reluctant matching, as it will automatically stop at the first { .

What about this regex:

PROCEDURE\s+\w+\s*\{(?:.*?\{.*?\})*.*?\}

It matches pairs of {}.

However, if the procedure contains strings or comments that contain curly brackets it will fail.

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