得到递归使用正则表达式C#内部图案递归、图案、正则表达式

2023-09-02 01:59:14 作者:难解惆怅

我知道有关于.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.

第一个问题, 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
 
精彩推荐
图片推荐