I want to get the table name from a sql (for example:select name from user where id>5).
I try to use regular expression,my pattern is from\s+(.*)(\s*$|where)
When the sql does not include ‘where’,it is ok. But if the sql include ‘where’, I will get ‘user where id>5’ as answer.
Because a sql may not have ‘where’, so I use $, but (.*)(\s*$) almost can match everything, I try to use this from\s+(.*)(\s*where|$), I get the same wrong answer.
I wonder if there is a order to match when I use |?
how should I do?
your
.*first eats everything, including the whereclause, and THEN it matches the$. The cause is the greedy*You should append it with the lazy ?
Try
FROM\s+(.*?)(\s+WHERE|$)