I'm surprised I didn't find a duplicate of this question. I'm trying to parse out some sql strings and replace the first "SELECT... FROM " string with a different string. I would think it wouldn't be too difficult but haven't hit on the solution yet. If my test string is:
' SELECT something, something_else FROM (SELECT somebody FROM some_table)'
and I want to replace it with:
' SELECT count(*) as total FROM (SELECT somebody FROM some_table)'
I've tried the following regex expressions using REReplaceNoCase():
<cfset SQL = #REReplaceNoCase(test, "^\s*(SELECT.*\s+FROM\s)??", "SELECT count(*) as total FROM ")# />
<cfset SQL = #REReplaceNoCase(test, "^\s*SELECT.*(\s+FROM\s)??", "SELECT count(*) as total FROM ")# />
<cfset SQL = #REReplaceNoCase(test, "^\s*SELECT.*\s+(FROM)??\s", "SELECT count(*) as total FROM ")# />
as well as some other variations. Suggestions?