Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Regex switching problem

by cfreak (Chaplain)
on Oct 20, 2001 at 01:38 UTC ( #120159=perlquestion: print w/replies, xml ) Need Help??

cfreak has asked for the wisdom of the Perl Monks concerning the following question:

I can't say I'm the world's best at regex's but I can usually get them to do as I wish, however today this does not seem to be the case. here's my problem. I'm trying to do a simple search on a database. I would like to be able to recognize 'OR' and 'AND'. I simply want to translate this into an SQL query. Starting out I thought that should be simple but it is proving rather difficult.

After failing miserably in my main program I wrote the following test script, really ugly but I'm also trying to see everything:

#!/usr/bin/perl use strict; my $string = "something and something else or this thing here"; my @out = split(\s(AND|OR)\s/i,$string); $string = ""; foreach my $i(@out) { if($i =~ /\s?(AND|OR)\s?/i) { my $tmp = uc($1); $string .= " $tmp"; } else { $i =~ s/$i/ tools.description LIKE "\%$i%"/o; $string .= $i; } } print "$string\n";

Consistantly the first element in the array gets switched and the rest of them do not, as though the line isn't even there they simply get appended to $string the way they were entered. The output looks like this:

tools.description LIKE "%Something%" ANDsometing else ORthis thing her +e

Printing each element of the array as it loops reveals that the array elements are as expected:

Something and something else or this thing here

I'm at a complete loss with this one. I'm also interested in simplifying it into something shorter but whatever helps to solve this problem... thanks in advance.

Replies are listed 'Best First'.
Re: Regex switching problem
by blakem (Monsignor) on Oct 20, 2001 at 01:57 UTC
    This line:
    my @out = split(\s(AND|OR)\s/i,$string);
    looks screwy to me. Perhaps you meant:
    my @out = split(/\s(AND|OR)\s/i,$string);
    Update: Here is a quick rewrite that I believe does what you want...

    #!/usr/bin/perl -wT use strict; my $string = "something and something else or this thing here"; my $new = join(' ', map {/^(and|or)$/ ? uc($_) : "tools.description LIKE \"%$ +_%\""} split(/\s*\b(and|or)\b\s*/,$string)); print "S:$string\n"; print "N:$new\n"; =OUTPUT S:something and something else or this thing here N:tools.description LIKE "%something%" AND tools.description LIKE "%so +mething else%" OR tools.description LIKE "%this thing here%"
    Reading the complicated line from bottom to top.
    • We split the string into a list of and/or/other pieces.
    • Then convert that list using map, uppercasing and/or and putting some surrounding text around other.
    • Finally we join the list back together.

    -Blake

Re: Regex switching problem
by nardo (Friar) on Oct 20, 2001 at 02:01 UTC
    Do you understand what the /o regex modifier does? The line:
    $i =~ s/$i/ tools.description LIKE "\%$i%"/o;
    This will compile the regex only once so whatever the first $i is becomes what is used for all subsequent matches, I don't think you mean to use /o. It's also a bit silly to use $i =~ /$i/ logic.
      Ahhh of course... I think its lack of sleep or something. One of my co workers pointed out that there was really no reason to switch the whole string... duh okay code perl after more sleep next time :)

      Thanks everyone

Re: Regex switching problem
by jackdied (Monk) on Oct 20, 2001 at 02:00 UTC
    Updated, how bout this

    my @out = split(/\s+(AND|OR)\s+/i, $string); for (my $i = 0; $i < scalar(@out); $i += 2) { $out[$i] = 'tools.description LIKE "%' . $out[$i] . '%"'; #NB, you w +ant to quote $out[$i] appropriately for your database to avoid arbitr +ary user SQL uc($out[$i+1]); } print join(' ', @out);
    -jackdied

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://120159]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-06-26 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (86 votes). Check out past polls.

    Notices?