Lookahead regex issue eating the last letter
My Code
preg_match_all('/\{([\w]+)(\s.*)?[^\}]\}(.*)\{\/\w+[^\}]\}/', $nbody, $matches );
My Template
rtrim: Beginning Text{rtrim} String {/rtrim}Surrounding Text
camelize: {camelize}Camelize Some Text Like A MediaWiki Title{/camelize}
Date Modifiers
date: {date format="M/d/Y"}June 14th 1965{/date}
iso date: {date format="c"}1310036802{/date}
timestamp date: {date format="c"}June 14th 1965{/date}
time: {time}{/time}
I am trying to get my regex to work so that I can capture the attributes as seen with the {date format=""} example. With the above pattern I'm 99% of the way there but it eats the last character of {tags} without attributes.
...
[11] => rtri
[12] => cameliz
[13] => date
[14] => date
...
As well as the last " of the attribute matches
...
[13] => format="M/d/Y
[14] => format="c
[15] => format="c
...
Any help would be a开发者_C百科ppreciated!
this expr would probably fit better
~{(\w+)(.*?)}(.*?){/\\1}~si
To prevent it from "eating" those chars remove the random [^\}]
. But I guess you meant to write a better regex with help of them, maybe something like:
/\{(\w+) (?: \s+(\S[^}]*))? } (.*?) \{\/ \1 }/x
(Quote and escape it properly before use.)
That should work a lot better. Will still fail on nested tags (but if you have that then you are parsing HTML with regex), also will not work if the content of the tag can have }
(that can be fixed if needed).
精彩评论