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.


In reply to Regex switching problem by cfreak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.