0

I have a regex.

(\w+(?==\w+)|(?!==\w+)\w+)(?!\{\{)(?!.*token)(?=.*}})

Test string:

some text {{token id=foo1 class=foo2 attr1=foo3 hi=we}} more text {{token abc=xyz}} final text

But, this matches only {{token abc=xyz}} and not the {{token id=fool class=foo2 attr1=foo3 hi=we}}

I've already used g modifier.

How can I modify to match the first one also?

Amit Joki
  • 58,320
  • 7
  • 77
  • 95

1 Answers1

2

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)
 )