I would like to highlight SQL keywords that occur within a string in a syntax highlighter. Here are the rules I would like to have:
- Match the keywords SELECT and FROM (others will be added, but we'll start here). Must be all-caps
- Must be contained in a string -- either starting with
'
or"
- The first word in that string (ignoring whitespace preceding it) should be one of the keywords.
This of course is not comprehensive (can ignore escapes within a string), but I'd like to start here.
Here are a few examples:
- SELECT * FROM main -- will not match (not in a string)
- "SELECT name FROM main" -- will match
- "
SELECT name FROM main" -- will match - """Here is a SQL statement:
SELECT * FROM main""" -- no, string does not start with a keyword (SELECT...).
The only way I thought to do it in a single regex would be with a negative lookbehind...but then it would not be fixed width, as we don't know when the string starts. Something like:
But this of course won't work:
Would something like this be possible to do in a single regex?