简体   繁体   English

正则表达式匹配括号内的所有内部括号?

[英]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 } . 您需要在第first {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 { . 我在字符类中包含花括号,这样你就不需要使用[^{]*来转义它们,你不需要reluctant匹配,因为它会自动停在第一个{

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. 但是,如果过程包含包含大括号的字符串或注释,则它将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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