これがあなたのニーズを満たすかもしれないパターンです:
^\[!((?<n>\w+='\[!)|(?<inner-n>!]')|\w+='(?!\[!)[^']*'| )*!](?!(n))$
各アイテムの最も内側のアイテムを順番に表示します。私が何を意味するのかを説明するために、コードを考えると:
[!a='test' c='[!x='blah'!]' b='[!a='[!y='innermost'!]' b='innervalue'!]' !]
次の一致が得られます(グループ「内部」のキャプチャコレクション内)。
x='blag'
y='innermost'
a='[!y='innermost'!]' b='innervalue'
したがって、の各x=y
アイテムについて[! .. !]
、最も内側から外側に向かって順番に一致が表示されます。
式全体もキャプチャする場合は、次のように変更できます。
^(?<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'!]'
そして、正規表現を説明するには:
^ # 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