如何使用Regex解析嵌套块? [英] How can I parse nested blocks using Regex?

查看:84
本文介绍了如何使用Regex解析嵌套块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
RegEx匹配除XHTML自包含标签之外的其他开放标签
.NET Regex平衡组表达式-不平衡时进行匹配

Possible Duplicates:
RegEx match open tags except XHTML self-contained tags
.NET Regex balancing groups expression - matching when not balanced

例如,如果我有输入:

[quote]He said:
    [quote]I have no idea![/quote]
But I disagree![/quote]

And another quote:

[quote]Some other quote here.[/quote]

如何使用正则表达式有效地抓住引号,而又不会抓住太多或太少?例如,如果我使用:

How can I effectively grab blocks of quotes using regular expressions without grabbing too much or too little? For example, if I use:

\[Quote\](.+)\[/Quote\]

这会抢太多(基本上是整个东西),而这是

This will grab too much (basically, the entire thing), whereas this:

\[Quote\](.+?)\[/Quote\]

抓得太少(仅抓取[quote]He said:[quote]I have no idea![/quote],且起始/结尾花括号不匹配).

will grab too little (it will only grab [quote]He said:[quote]I have no idea![/quote], with mismatching start/end braces).

那么我该如何使用Regex有效地解析嵌套的代码块呢?

So how can I effectively parse nested blocks of code like this using Regex?

推荐答案

看看我的

Take a look at my xml indenter, it uses groups to match beginning tag to the last tag, and another group to get the content recursively.

这篇关于如何使用Regex解析嵌套块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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