in reply to Regular Expression Question !!!

I'd try a different approach.

$sql = 'select * from EMP where EMPNO=&eno and DEPTNO=&dno and JOB=&jo +b'; my @fields; if (my($where) = $sql =~ /\swhere\s+(.*)/i) { @fields = map /(\S*)=&/, split /\sand\s/i, $where; }
(In English: parse out the "where" expression. Split it into terms on the "and" keyword. For each term, see if it matches the pattern of an identifier followed by "=&" and keep the identifier if it does match.)

Hmm, are you sure you want the "=" to be part of the match?

p.s. This is in my opinion a somewhat fishy approach, as it depends strongly on the query being formatted in a specific way. This might suffice for your needs, if the SQL is indeed as restricted as it appears here. In other cases, I'd look at better SQL parsers, like SQL::Parser, or Ovid's article on tokenizing SQL on perl.com

Replies are listed 'Best First'.
Re^2: Regular Expression Question !!!
by slg_saravanan (Initiate) on Jun 20, 2007 at 12:17 UTC
    I tried as follows..

    $string = "select * from EMP where EMPNO=&eno and DEPTNO=&dno and JOB= +&job"; @arr = split('\s+',$string); foreach $i (@arr) { if($i =~ /\&/g) { @arr1 = split('&',$i); print $arr1[0]."\n"; } }


    its working fine..Thanks a lot for the support pals !! :)