Jump to content

[SOLVED] Tag parsing regex


sachavdk

Recommended Posts

Hi,

 

I have 2 regexes here:

 

#{view:[a-z]+(?::[a-z]+)?(?: [a-z]+=["'].*?["'])*}#is

and

#{view:[a-z]+(?::[a-z]+)?(?: [a-z]+=["'].*?["'])* /}#is

 

the first one to match

{view:food:cheese type="old"}

or

{view:car}

or

{view:car engine="boxer"}

 

the second to match

{view:food:cheese type="old" /}

or

{view:car /}

or

{view:car engine="boxer" cc='2394' /}

 

The first regex matches correctly the first three tags, but it also matches the second three tags with the close slash and I can't figure out what I'm doing wrong.

 

Anyone else maybe sees it?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/100675-solved-tag-parsing-regex/
Share on other sites

The first works for me, matching 3 start tags, but the second matches multiple. It continually backtracks into (?:...), attempting to satisfy a match.

 

For example: {view:food:cheese type="old" matches, but /} does not follow; therefore, it gives up " and continues moving forward. You can prevent this by converting (?:...) to (?>...) (an atomic match), or, even better, change .*? to [^"']*.

 

But I'd never thought the backtracking would give this "problem".

 

The main goal of every pattern is to try to find a match, which includes backtracking wherever possible. This is prevented by the two methods above: either modifying the pattern, or using atomic grouping.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.