On Mon, 25 Jan 2016 16:16:40 -0800
SSC_perl <p...@surfshopcart.com> wrote:

>       I'm trying to find a way to trap bad item numbers.  I want to
> parse the parameter "itemid=" and then everything up to either an "&"
> or end-of-string.  A good item number will contain only ASCII
> letters, numbers, dashes, and underscores and may terminate with a
> "&" or it may not (see samples below).   The following string should
> test negative in the regex below:
> 
> my $QUERY_STRING = 'itemid=AT18C&i_AT18C=1';
> 
> but a string containing "itemid=AT18/C" should test positive, since
> it has a slash.
> 
>       I can catch a single bad character and get it to work, e.g.
> 
> if ( $QUERY_STRING =~ m| itemid= .*? [/]+? .*? &? |x ) {
> 
> but I'd like to do something like this instead to catch others:
> 
> if ( $QUERY_STRING =~ m| itemid= (?: .*? [^a-zA-Z0_-]+ .*? ) &? |x )
> { ...
> 
>       Unfortunately, I can't get it to work.  I've read perlretut,
> but can't see the answer.  What am I doing wrong?
> 
> Thanks,
> Frank
> 
> Here are a couple of test strings:
> 
> 'itemid=AT18C&i_AT18C=1&t=main.htm&storeid=1&cols=1&c=detail.htm&ordering=asc'
> 
> 'c=detail.htm&itemid=AT18C'
> 
> 
> 
> 

Use the negative match operator !~

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


-- 
Don't stop where the ink does.
        Shawn

-- 
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