title: Parsing SQL statements with regex: Does adding the optional flag in a regex expression change how greedy or posessive modifiers are matched?
body: <p>I'm creating a relational database with SQL syntax which will have a command line interface using Java. To parse the user commands, I'm using regular expressions (I know it's a terrible idea - It's more to learn than anything else)<br>
  I'm using the scanner class with the semicolon as the delimiter, so I don't look for semicolons in my regex. This way it can handle multiline input. The input is trimmed before being matched against the regular expression, so whitespace at the beginning and end of the string are not part of the expression.</p>
  
  <p>I've got this regex here which works the way I want it to, until I add the optional flag for the where clause- </p>
  
  <pre><code>select\s+(.*?)(?:\s+where(.*))
  </code></pre>
  
  <p>That would match (groups are bolded) - </p>
  
  <p>select <strong>a from *</strong> where <strong>x = 3</strong></p>
  
  <p>But if I change the regular expression to this -  </p>
  
  <pre><code>select\s+(.*?)(?:\s+where(.*))?
  </code></pre>
  
  <p>It only matches the quoted part -</p>
  
  <pre><code>"select "a from * where x = 3
  </code></pre>
  
  <p>My goal is to have it match everything until the end of the string after the word 'select' (including whitespace characters) unless there is a string 'where' preceded and followed by any whitespace characters. If that is present, then group every character between the word 'select' and 'where' and also group everything after the word 'where'.   </p>
  
  <p>For example:
  If this text was input:</p>
  
  <pre><code>select a from * where b = 3
  </code></pre>
  
  <p>It should group " a from * " and " b = 3 ".  </p>
  
  <p>But if this was the input:</p>
  
  <pre><code>select a where x = 3
  </code></pre>
  
  <p>The table name "a" should be a group and the where clause "x = 3" should be a group. </p>
  
  <p>It is important to note I'm using java.util.regex - It does not have if/else clauses as in Perl regex, but lookaheads with or statements inside a group could be used for the same effect. I could use another library which has support for if/then/else statements, but I can't figure out I could use that to achieve the result I'm looking for.</p>