Nahydrin
Nahydrin

Reputation: 13517

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 escaping 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.

Upvotes: 1

Views: 68

Answers (1)

user684934
user684934

Reputation:

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.

Upvotes: 2

Related Questions