Some good answers to your question have already been made, but I think there is some work to do on understanding the regular expression you propose.

First, refer to the part '*?' that you used. The intent of the '?' modifier is to change the match from 'as many as possible' to 'as few as possible'. So, you get FROM table1 WHERE, the shortest match meeting the requirements.

If you remove the question mark, you would get 'FROM table1 WHERE ..." SQL FROM table2 WHERE' instead. Note that this has nothing to do with the quotes, they are just another character in the string.

So, if you want to do the shortest match and find all possible matches, you need to iterate through the string. You do that using the 'global' modifier in a scalar context:

$str='SELECT "SELECT * FROM table1 WHERE ..." SQL FROM table2 WHERE'; print "$1\n" while $str =~ /(\bFROM\b.*?\bWHERE\b)/g;

The result is two lines:

FROM table1 WHERE FROM table2 WHERE


In reply to Re: REGEX search for words not in quotes. by rmcgowan
in thread REGEX search for words not in quotes. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.