"SSC_perl" wrote in message news:ef7499af-b4a5-4b07-8c69-3192ef782...@surfshopcart.com...

On Jan 25, 2016, at 4:59 PM, Shawn H Corey wrote:

Use the negative match operator !~

 if( $QUERY_STRING !~ m{ itemid = [-0-9A-Za-z_]+? (?: \& | \z ) }msx ){
   print "bad: $QUERY_STRING\n";
 }

Thanks for that, Shawn. It works perfectly except for one criteria that I inadvertently forgot to >include. It's possible that the string will _not_ contain the itemid parameter at all. When that's >missing, the regex matches and it shouldn't. I guess that's why I was trying to stay with the >positive match operator.

I tried inverting your regex:

if ( $QUERY_STRING =~ m/ itemid= .*? [^-0-9A-Za-z_]+? .*? (?: \& | \z ) /sx ) {
 > say "bad: $QUERY_STRING";
}

but that doesn't work either.  It catches even good item numbers.

In the meantime, I got it to work by grabbing the itemid and working with that separately:

my $item_id = $1 if ($QUERY_STRING =~ m/ itemid=([^&]*) /x);
if ( $item_id =~ m/ [^a-zA-Z0-9_-] /x ) { ...

however, I'd like to do that with a single line, if possible, so I don't have to create a new variable >just for that.

Thanks,
Frank=

###############################################################################
###############################################################################

Hello Frank,

You could do that in 1 line - See the following small program.
(The line using a 'grep' solution is commented out. It would work as well).


#!/usr/bin/perl
use strict;
use warnings;

while (my $id = <DATA>) {
   chomp $id;
   #if (grep /itemid=.*?[^\w-]/, split /&/, $id) {
   if ($id =~ /itemid/ && $id !~ /itemid=[\w-]+(?:&|$)/) {
       print "Bad id: <$id>\n";
   }
}

__DATA__
itemid=AT18C&i_AT18C=1&t=main.htm&storeid=1&cols=1&c=detail.htm&ordering=asc
c=detail.htm&itemid=AT18C
itemid=AT18/C
t=main.htm&storeid=1&cols=1&c=detail.htm&ordering=asc
itemid=?AT18C


When this is run, it prints out:

Bad id: <itemid=AT18/C>
Bad id: <itemid=?AT18C>

Chris

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to