in reply to REGEX search for words not in quotes.

$a = qq[SELECT "SELECT * FROM table1 WHERE ..." SQL FROM table2 WHERE +...];; $a =~ /(?:"[^"]+")?.*(\bFROM\b.*?\bWHERE\b)/g and print $1;; FROM table2 WHERE

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: REGEX search for words not in quotes.
by Anonymous Monk on Apr 08, 2010 at 22:25 UTC
    Thanks! I worked up this mess
    #! use Smart::Comments; use strict; use warnings; my $np; $np=qr/ (?:\( # Capture the opening "(" (?: # '(?:[^']|\')*?' #' a single quote string |"(?:[^']|\")*?" #" a double quote string | [^()] # not a parentheses | (??{$np}) )* \)) # and the closing ")" /ix; my $string; my $re; $string=<<'__EOString__'; SELECT "SELECT * FROM table1 WHERE ...",'FROM WHERE' SQL FROM +table2 join(select from here to where eternity) WHERE ... __EOString__ $re=qr/ ^([^'"(]* (?:"(?:[^']|\")*?"|'(?:[^']|\')*?'|$np)? )* [^'"(]* (\bFROM\b ([^'"(]* (?:"(?:[^']|\")*?"|'(?:[^']|\')*?'|$np)? )* [^'"(]*? \bWHERE\b) /ix; # works ### $string while ($string =~ m{$re}ig) { ### @- ### @+ ### $1 ### $2 ### $3 }; exit;
    yielding ...
    ### $string: ' SELECT "SELECT * FROM table1 WHERE ..." "FROM WH +ERE"SQL FROM table2 join(select my mother from here to where eternity +) WHERE ...' Complex regular subexpression recursion limit (32766) exceeded at RE.p +l line 45. ### @-: '0', ### '64', ### '64', ### '127' ### @+: '132', ### '64', ### '132', ### '127' ### $1: '' ### $2: 'FROM table2 join(select my mother from here to where eternity +) WHERE' ### $3: ''

      Is that:

      SELECT "SELECT * FROM table1 WHERE ...",'FROM WHERE' SQL FROM table2 j +oin(select from here to where eternity) WHERE ...

      valid syntax anywhere?

      I'm betting the answer is no. So why the hell are you trying to cater for it?

      My house could be struck by a meteorite--it happens--but even the most risk adverse, paranoid people on the planet--insurance companies--do not require me to indemnify against it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Trust me, sir! Unfortunately we do!