sachavdk Posted April 11, 2008 Share Posted April 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/100675-solved-tag-parsing-regex/ Share on other sites More sharing options...
effigy Posted April 11, 2008 Share Posted April 11, 2008 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 [^"']*. Quote Link to comment https://forums.phpfreaks.com/topic/100675-solved-tag-parsing-regex/#findComment-514965 Share on other sites More sharing options...
sachavdk Posted April 11, 2008 Author Share Posted April 11, 2008 Using [^"']* indeed did the trick. But I'd never thought the backtracking would give this "problem". Anyway, many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/100675-solved-tag-parsing-regex/#findComment-515262 Share on other sites More sharing options...
effigy Posted April 14, 2008 Share Posted April 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100675-solved-tag-parsing-regex/#findComment-516705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.