Get internal patterns recursively using regex c #

I know there are several questions about regular expression recursion in .net. I can write some complex regular expression expressions, but this recursion is taller than me, I just can't write it.

These are the questions closest to what I want.

first question, second question .

but it matches the whole line, I want the matches in the collection to be preferable to the sleepiest match first or in some order. It also matches one opening symbol and one closing symbol. Mine - 2 characters for opening and closing, [! and!]

my input line will be something like this.

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

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

Can anyone help with this?

+7
source share
1 answer

Here is a template that can suit your needs:

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

It will provide 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 "inner" group):

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

So, for each element x=y in [! .. !] [! .. !] he will give coincidences in order from the innermost outward.

If you want the general expression to be captured, you can change it as follows:

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

Donation:

 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 
+11
source

All Articles