匹配嵌套大括号的正则表达式 [英] regular expression to match nested braces

查看:64
本文介绍了匹配嵌套大括号的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要正则表达式来匹配正确的大括号,例如每个打开一个关闭一个abc{abc{bc}xyz} 我需要它从 {abc{bc}xyz} 得到它而不是 {abc{bc}.

I need regular expression to match braces correct e.g for every open one close one abc{abc{bc}xyz} I need it get all it from {abc{bc}xyz} not get {abc{bc}.

我尝试使用 (\{.*?})

推荐答案

这对于正则表达式是不可能的.为此需要上下文无关文法,而正则表达式仅适用于有限的正则语言.

This is not possible with regular expressions. A context-free grammar would be necessary for this and regular expressions only work for finite regular languages.

根据此链接,有一个扩展可用.NET 中的正则表达式可以做到这一点,但这仅仅意味着 .NET 正则表达式不仅仅是正则表达式.

According to this link there is an extension available for the regular expressions in .NET that can do this, but this just means that .NET regular expressions are more than just regular expressions.

这篇关于匹配嵌套大括号的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆