Regular expression to match single bracket pairs but not double bracket pairs

后端 未结 2 1383
盖世英雄少女心
盖世英雄少女心 2021-01-19 12:11

Is it possible to make a regular expression to match everything within single brackets but ignore double brackets, so for example in:

{foo} {bar} {{baz}}
         


        
2条回答
  •  感情败类
    2021-01-19 12:57

    In many languages you can use lookaround assertions:

    (?

    Explanation:

    • (?: previous character is not a {
    • \{([^}]+)\}: something inside curly braces, e.g. {foo}
    • (?!\}): following character is not a }

提交回复
热议问题