Replacing tags in braces, even nested tags, with regex

前端 未结 3 1161
感情败类
感情败类 2021-01-26 08:36

Example

preg_replace(\'/\\{[a-zA-Z.,\\(\\)0-9]+\\}/\', \'Replaced\', \'Lorem ipsum dolor sit {tag1({tag2()})}, consectetur adipiscing elit.\');
         


        
3条回答
  •  日久生厌
    2021-01-26 09:29

    Once you start talking about matching nested patterns (eg: matching the inner bracketed group in something like (foo (bar) fu)), then regex is the wrong tool. Regular Expressions are stateless, which, in this case, means that they can't count how many brackets are open.

    If you are looking to do something like that, you might need to look into a parser

提交回复
热议问题