Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Regex is not working

by Rahul Gupta (Sexton)
on Oct 03, 2013 at 04:56 UTC ( #1056719=perlquestion: print w/ replies, xml ) Need Help??
Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:


I am trying to substitute values in query but things are not working for me.

Here is my code

my @array_1 = ("\$Id_run_query=1", "\$name_run_query=xyz", "db_query_r +un_query=select * from meter where metetid=\$Id and metername=\$name" +); my $query; foreach(@array_1){ if ($_ =~ m/^db_query_run_query=(.*)/i){$query = $1;print "$1\n";} } foreach(@array_1){ if ($_ =~ m/^(\$\S+)=(.*)/i){ my $replace = $2; my $value = shift @{ [ split( /_/, $_ ) ] }; print "$value\n"; chomp($value); # print "$query\n"; $query =~ s/$value/$replace/i; } } print "===>$query\n";

the expected output is :

"select * from meter where metetid=1 and metername=xyz"

but i am geeting this:

"select * from meter where metetid=$Id and metername=$name"

Thanks in advance

Comment on Regex is not working
Select or Download Code
Re: Regex is not working
by moritz (Cardinal) on Oct 03, 2013 at 05:21 UTC
    $query =~ s/$value/$replace/i;

    The problem is that $value contains something like $Id, and the $ when interpreted as a regex means "match the end of the string", not "match a dollar sign". You can fix that by writing



    Fwiw this is how I'd solve the same task:

    my @array_1 = ("\$Id_run_query=1", "\$name_run_query=xyz", "db_query_r +un_query=select * from meter where metetid=\$Id and metername=\$name" +); my $query; my %replace; foreach(@array_1){ if (m/^db_query_run_query=(.*)/i){ $query = $1 } elsif (/(\$\S+)_run_query=(.*)/) { $replace{$1} = $2; } } my $replace_re = join '|', map quotemeta, keys %replace; $query =~ s/($replace_re)/$replace{$1}/ig; print "===>$query\n";

    The call to quotemeta does the same as \Q...\E in the regex.

      Thanks it worked for me:-)

Re: Regex is not working
by kcott (Abbot) on Oct 03, 2013 at 06:18 UTC

    G'day Rahul Gupta,

    Your underlying problem is that the value of $value in $query =~ s/$value/$replace/i is being interpolated: $Id and $name evaluate to zero-length strings so $query =~ s/$value/$replace/i is equivalent to $query =~ s//$replace/i (i.e. no substitutions occur). That was a poor guess on my part; see moritz' explanation instead; the fixes, however, are identical. To fix, use

    $query =~ s/\Q$value\E/$replace/i;

    -- Ken

Re: Regex is not working
by CountZero (Bishop) on Oct 03, 2013 at 16:06 UTC
    If this query is to be used in a DBI environment to get data out of the database, wouldn't it be much safer (and probably easier too) to simply use "?" placeholders?


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056719]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (21)
As of 2014-10-20 19:06 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (89 votes), past polls