java - Parsing SQL statements with regex: Does adding the optional flag in a regex expression change how greedy or posessive modifiers are matched? -


i'm creating relational database sql syntax have command line interface using java. parse user commands, i'm using regular expressions (i know it's terrible idea - it's more learn else)
i'm using scanner class semicolon delimiter, don't semicolons in regex. way can handle multiline input. input trimmed before being matched against regular expression, whitespace @ beginning , end of string not part of expression.

i've got regex here works way want to, until add optional flag clause-

select\s+(.*?)(?:\s+where(.*)) 

that match (groups bolded) -

select a * x = 3

but if change regular expression -

select\s+(.*?)(?:\s+where(.*))? 

it matches quoted part -

"select "a * x = 3 

my goal have match until end of string after word 'select' (including whitespace characters) unless there string 'where' preceded , followed whitespace characters. if present, group every character between word 'select' , 'where' , group after word 'where'.

for example: if text input:

select * b = 3 

it should group " * " , " b = 3 ".

but if input:

select x = 3 

the table name "a" should group , clause "x = 3" should group.

it important note i'm using java.util.regex - not have if/else clauses in perl regex, lookaheads or statements inside group used same effect. use library has support if/then/else statements, can't figure out use achieve result i'm looking for.

parsing sql regex not different parsing html regex. in other words, won't work. hopeless task, stop right now.

instead, use sql parser. example, sql::statement::structure perl or antlr java.

also, since creating own database, worth taking @ how other sql implementations it. recommend reading source code postgresql or mysql , see how implement advanced sql parsing.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -