I have an array that looks like this
{
...
{
OwnValues=
{
key1,
key2,
key3
},
ReferencedValues=
{
key10,
key11,
key12,
key17
}
},
{
OwnValues=
{
key10,
key20,
key30
},
ReferencedValues=
{
key2,
key55,
key17
}
},
{
OwnValues=
{
key15,
key17,
key99
},
ReferencedValues=
{
key33,
key55,
key52
}
},
...
}
I want to match key17
as a direct child of OwnValues
.
I tried this naive look behind
(?<=OwnValues=)([\s\S]*key17)
But this does not work, it matches a block between the first occurence of OwnValues=
and the last occurence of key17
. I would like to exclude ReferencedValues
inbetween. What is the proper way to regexpr this?
CodePudding user response:
You could match {
and then use a negated character to match key17 without crossing matching curly's.
If you want the capture group, then you can use:
\bOwnValues=\s*{[^{}]*\b(key17)\b
\bOwnValues=
Match the word OwnValues followed by=
\s*{
Match optional whitespace chars and then match{
[^{}]*
Match 0 times any char except{
and}
\b(key17)\b
Capture the wordkey17
between word boundaries
See a regex demo.
Or if supported a quantifier inside a lookbehind assertion to get a match only:
(?<=\bOwnValues=\s*{[^{}]*)\bkey17\b
See another regex demo.
CodePudding user response:
This regex matches key17
in the context you've described:
OwnValues=[^=]*?(key17)
It merely matches everything that's before the next =
character, to make sure the value is within the same section.