Whimusical
Whimusical

Reputation: 6629

Why is that REGEX not working?

.*?(?<=(?:FROM|JOIN))\s*(.*?)\s*(?=(?:ON|JOIN|WHERE|LIMIT|GROUP|ORDER|\(|;))

INPUT:

SELECT * FROM (SELECT * FROM table1, table2 WHERE bla,bla GROUP BY bla ORDER bla LIMIT bla) tableTmp UNION SELECT * FROM table3,table4 WHERE bla;

http://regex101.com/r/cE7tT8

I must be missing something, last part is not eaten by the wildcard... It outputs: "table1, table2table3,table4WHERE bla;"

I tried combinations with '$' but either it does not match anything or either it leaves intact the last part

Upvotes: 0

Views: 47

Answers (1)

Taemyr
Taemyr

Reputation: 3437

Note that the link you provide does several substitutions. It makes three matches and does subs for each of them. You can see this by substituting with |\0| instead of your \1.

So the problem is that you have junk left over at the end after all these have been matched. A solution is to have the match consume characters up to the beginning of relevant portions of the next match.

This can be done by using negative lookahead. - Add (.(?!SELECT))* to the end of your regexp;

.*?(?<=(?:FROM|JOIN))\s*(.*?)\s*(?=(?:ON|JOIN|WHERE|LIMIT|GROUP|ORDER|\(|;))(.(?!SELECT))*

Upvotes: 1

Related Questions