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
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 [^"']*.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.