查找与正则表达式匹配的支架 [英] Find matching bracket with Regex

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

问题描述

的输入是一串重presenting元素的列表。

The input is a string representing a list of elements.

一个列表定义为一个开放的大 {后跟0个或更多的元素以空格后跟一个封闭的大分离}

A list is defined as an open curly { followed by 0 or more elements separated by whitespace followed by a closed curly }.

这是元素可以是一个文字或元素列表

An element is either a literal or a list of elements.

一个文字是无空格字符的继承。如果一个元素包含了一个大括号,必须用反斜线转义: \ { \} 。 (或者你可以假设花括号不是里面文字不允许,为简单起见)

A literal is a succession of non-whitespace characters. If an element contains a curly bracket, it must be escaped with a backslash : \{ and \}. (Or you could assume curlies are not allowed inside literals, for simplicity)

例如:

"{abc { def ghi } 7 { 1 {2} {3 4} } {5 6} x\{yz \}foo }"

里面的文字不花括号:

No curlies inside literals:

"{abc { def ghi } 7 { 1 {2} {3 4} } {5 6} xyz foo }"

(这是一个Tcl列表一个简单的定义。)

(This is a simplified definition of a Tcl list.)

我想知道的是:可以输入拆分成最外层循环的使用正则表达式的元素

What I want to know is: can the input be split into the elements of the outermost loop using regex?

期望的输出:

abc
{ def ghi }
7
{ 1 {2} {3 4} }
{5 6}
x{yz
}foo

真正的问题是:可以这样用正则表达式做了什么?

The real question is: can this be done with a Regex?

我最感兴趣的是.NET的味道,但会接受任何答案。

I'm most interested in the .NET flavour, but will accept any answers.

我会后我自己的假设,答案,看看它是否有效或销毁。

I'll post my own assumption in an answer, and see if it's validated or destroyed.

推荐答案

好了,编辑会删除标记大括号,并采取刺的问题,现在是很容易可行与.net的正则表达式,使用均衡组。它简单地匹配的括号,这是一个基本的例子。
就像KennyTM的回答,如果删除顶级括号,这只会工作,或者它会匹配整个输入。
再次,这是更好地用于娱乐目的:

Well, the edit removes curly braces from tokens and takes the sting from the question, and now it is easily doable with .Net Regexes, using balancing groups. It is simply matching braces, which is a basic example.
Much like KennyTM's answer, this will only work if you remove the top level braces, or it will match the whole input.
Again, this is better used for recreational purposes:

(?:                    # try matching...
    (?:\\[{}]|[^\s{}])+\s*? # a literal (allow escaped curly braces)
    |                       # OR
    (?<Curly>{)\s*          # "{" and push to stack
    |                       # OR
    (?<-Curly>})\s*?        # "}", pop from stack and fail if the stack is empty
)+?                    # ...a few times, and stop whenever you can.
(?(Curly)(?!))         # Make sure there aren't any extra open curly braces

有关更多详细信息,请参阅这篇文章:正则表达式均衡组深度报道

For much more details see this article: Regex Balancing Group in Depth

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

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