PHP: Determine regex order of importance
I've hit a snag in a syntax highlighter I've been working on. PHP supports strings contained between both ""
and ''
. Unlike C#, which is more easily controlled by only allowing ""
, with the option @
for automatically e开发者_运维知识库scaping the string.
If I have two strings, like so:
$code = "print( \"<div class='test'>content?div><div class='test'>content</div>\" );";
If I echo $code, I will get:
print( "<div class='test'>content?div><div class='test'>content</div>" );
Here's my problem. When I parse content between the starting quote and ending quote, I removed such content to be put back later (so I don't cross highlight), using a clipboard tool (just copy paste). I have it setup to parse content between ''
first using regex. The end result (after both string types) will look like so:
print( "<div class=>content?div><div class=>content</div>" );
How can I modify my regex to look back (from start) and forward (from end) to look for the topmost string delimeter ('
or "
). It's hard to explain, hopefully someone gets it, otherwise I'll just have to live with it.
This is not possible with regular expressions; they are not powerful enough to deal with recursion. They cannot close nested parentheses correctly, they cannot close nested quotes correctly. You need a push-down automata (or a context-free language) at the least for this kind of functionality.
精彩评论