得到递归使用正则表达式C#内部图案 [英] get inner patterns recursively using regex c#

查看:156
本文介绍了得到递归使用正则表达式C#内部图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关于.NET正则表达式递归几个问题。我可以写比较复杂的正则表达式EX pressions但这递归是超越我,我只是不能写。

i know there are several questions about regex recursion in .net. I can write somewhat complex regex expressions but this recursion is beyond me, i am just not able to write it.

这是最接近的问题,我想要什么。

This are the questions closest to what i want.

第一个问题, <一href="http://stackoverflow.com/questions/1363776/capturing-inner-items-using-net-regex-balanced-matching">second问题。

但整个字符串匹配,我想在一个集合preferably比赛最里面的比赛第一或以某种顺序。此外,它在一个开放字符和一个结束字符匹配。我的是2个字符的开幕式和闭幕式,[!和!]

but it matches the entire string, i want the matches in a collection preferably the innermost match first or in some order. Also it matches on one opening character and one closing character. Mine is 2 chars for opening and closing, [! and !]

我输入的字符串会是这样的。

my input string will be something like this.

[!a='test' b='[!a='innertest' b='innervalue'!]'!]

我需要找到innertest部分, [!一个='innertest'B ='innervalue'!] ,然后再评估它通过我的前$之一p $ pssion树。然后评估母体一种含它。

I need to find the innertest section, [!a='innertest' b='innervalue'!], first and then evaluate it through one of my expression trees. then evaluate the parent one containing it.

谁能帮助呢?

推荐答案

下面是可能会满足您的需求模式:

Here's a pattern that might satisfy your needs:

^\[!((?<n>\w+='\[!)|(?<inner-n>!]')|\w+='(?!\[!)[^']*'| )*!](?!(n))$

这将给予每个项目的最里面的物品井然有序。为了解释我的意思,考虑到code:

It will give the innermost item for each item in order. To explain what I mean, given the code:

[!a='test' c='[!x='blah'!]' b='[!a='[!y='innermost'!]' b='innervalue'!]' !]

这会给下面的比赛(在捕捉收集组内部):

It will give the following matches (in the capture collection for the group "inner"):

x='blag'
y='innermost'
a='[!y='innermost'!]' b='innervalue'

因此​​,对于每个 X = Y 项在 [! ..!] ,它将从最里面向外给出为了比赛。

So, for each x=y item in the [! .. !], it will give the matches in order from innermost outwards.

如果你也想整个EX pression被捕获,你可以修改它是这样的:

If you also want the overall expression to be captured you can modify it like this:

^(?<n>\[!)((?<n>\w+='\[!)|(?<inner-n>!]')|\w+='(?!\[!)[^']*'| )*(?<inner-n>!])(?!(n))$

捐赠:

x='blag'
y='innermost'
a='[!y='innermost'!]' b='innervalue'
a='test' c='[!x='blag'!]' b='[!a='[!y='innermost'!]' b='innervalue'!]' 


和解释的正则表达式:


And to explain the regex:

^       # start of string
\[!     # start of overall [! .. !]
(       # either ...
    (?<n>\w+='\[!)|     # a complex x='[! .. !]' containing a nested [! .. !] - push this onto the stack 'n'
    (?<inner-n>!]')|    # end of a nested [! .. !] - pop stack 'n', and capture the contents into 'inner'
    \w+='(?!\[!)[^']*'| # a simple x='asdf' with no nested [! .. !]
     )                  # or a space
*       # as many times as you want
!]      # the end of the overall [! .. !]
(?!(n)) # assert that the 'n' stack is empty, no mismatched [! .. !]
$       # end of string

这篇关于得到递归使用正则表达式C#内部图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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