in reply to regex needed to remove parts of SQL

What about the following?
$sql =~ s/(select|,)\s*[^,]*?\s+as/$1/gi ;
Oguz

---------------------------------
life is ... $mutation = sub { $_[0] =~ s/(.)/rand()<0.1?1-$1:$1/ge };

Replies are listed 'Best First'.
Re^2: regex needed to remove parts of SQL
by jeanluca (Deacon) on Sep 12, 2007 at 10:40 UTC
    great!!, thats what I need.
    One last thing; what if I don't want to use $1 ? Should I split it in 2 expressions
    $sql =~ s/(?:SELECT)\s*[^,]*?\s+as/SELECT/gi ; $sql =~ s/,\s*[^,]*?\s+as/,/gi ;
    This works, except I don't understan why (?: .... ) is not working (I don't want to replace the word SELECT)

    LuCa
      In that case,
      $sql =~ s/(?<=select)\s*[^,]*?\s+as//gi ; $sql =~ s/(?<=,)\s*[^,]*?\s+as//gi ;
      Looks like the problem was that (?:) is non-capturing grouping. It stills eats up the characters. What you need is lookbehind (?<=).

      I also tried

      $sql =~ s/(?<=select|,)\s*[^,]*?\s+as//gi ;
      But the interpreter refused and said
      Variable length lookbehind not implemented in regex
      So, it looks like you have to have two regexes if you do not want to use $1.

      Oguz

      ---------------------------------
      life is ... $mutation = sub { $_[0] =~ s/(.)/rand()<0.1?1-$1:$1/ge };

        what about this
        $sql =~ s/((?<=select)|(?<=,))\s*[^,]*?\s+as//gi ;
        I couldn't have done this without your help!!

        thnx
        LuCa