This is in line to what you are trying to do
(Replace the .
with [\S\s]
if you want to span lines on the downstream check)
This first sollution can't validate {{token
exists
# /(?:(\w+)=(\w+))(?=(?:(?!{{token[ ]|}}).)*}})/
(?: # Next key,val pair
( \w+ ) # (1)
=
( \w+ ) # (2)
)
(?= # Downstream, must be a }}
(?:
(?! {{ token [ ] | }} )
.
)*
}}
)
Perl test case
$/ = undef;
$str = <DATA>;
while ( $str =~ /(?:(\w+)=(\w+))(?=(?:(?!{{token[ ]|}}).)*}})/g )
{
print "$1 = $2\n";
}
__DATA__
some text {{token id=foo1 class=foo2 attr1=foo3 hi=we}} more text {{token abc=xyz}} final text
Output >>
id = foo1
class = foo2
attr1 = foo3
hi = we
abc = xyz
This solution uses two regex's. One to grab the {{token .. }}
, one to parse key
=value
# Regex 1
{{ token \b
(?:
(?! {{ | }} )
.
)+
}}
# Regex 2
(?: # Next key,val pair
( \w+ ) # (1)
=
( \w+ ) # (2)
)