Re: Regex to match "bad" characters in a parameter
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 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex to match "bad" characters in a parameter
"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 = ) { chomp $id; #if (grep /itemid=.*?[^\w-]/, split /&/, $id) { if ($id =~ /itemid/ && $id !~ /itemid=[\w-]+(?:&|$)/) { print "Bad id: <$id>\n"; } } __DATA__ itemid=AT18C_AT18C=1=main.htm=1=1=detail.htm=asc c=detail.htm=AT18C itemid=AT18/C t=main.htm=1=1=detail.htm=asc itemid=?AT18C When this is run, it prints out: Bad id:
Re: Regex to match "bad" characters in a parameter
On Jan 26, 2016, at 11:22 AM, Chris Charley wrote: > > You could do that in 1 line - See the following small program. Thanks, Chris. That'll do the trick. And the grep alternative is interesting, too. I hadn't thought of that. Regards, Frank -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/