I'm trying to use eregi_replace() to replace the column list in a SQL SELECT query with the string "COUNT()". In other words, the query "SELECT username, email, password FROM users" would become "SELECT COUNT() FROM users".
I've devised a regex that works beautifully as long as the query only contains one SELECT statement:
$count_query = eregi_replace( "
^[ \t\r\n]* # zero or more whitespace characters
SELECT
[ \t\r\n]+ # one or more whitespace characters
.+ # one or more characters
[ \t\r\n]+ # one or more whitespace characters
FROM
[ \t\r\n]+ # one or more whitespace characters
(.+) # one or more characters - load into backreference \\1
$/sx", "SELECT COUNT(*) FROM \\1", $original_query );
However, I'm having problems with queries that contain SQL subselects. For example, the query "SELECT foo FROM bar WHERE baz IN ( SELECT bop FROM yyz )" produces "SELECT COUNT() FROM yyz )", which is not what I want. (It should be "SELECT COUNT() FROM bar WHERE baz IN ( SELECT bop FROM yyz )".)
I know this has to do with greediness, but that's one aspect of regular expressions that I don't know very well. At first, I thought all I had to do was change the line that reads ".+" to ".+?" (thus making that match ungreedy), but that produces a REG_BADRPT warning.
I putzed around with a few other ideas, but nothing is working. Can some kindly soul show me the error of my ways?