develooper Front page | perl.beginners | Postings from January 2002

Re: match + regexp synonyms?

Thread Previous
From:
Jeff 'japhy' Pinyan
Date:
January 30, 2002 14:07
Subject:
Re: match + regexp synonyms?
Message ID:
Pine.GSO.4.21.0201301702420.27903-100000@crusoe.crusoe.net
On Jan 30, ERIC Lawson - x52010 said:

>Why don't the match operators and regexps in the following produce the
>same results?
>
>	if ($editbl =~ /^\S+$/) { print AFILE "$editbl\n"; }
>	if ($editbl !~ /^\s*$/) { print EFILE "$editbl\n"; }

Your logic is off.  The first regex says:

"If $editbl matches 'start of string' followed by 'one or more
non-whitespaces' followed by 'end of string' then print it to AFILE."

The second regex says:

"If $editbl does NOT match 'start of string' followed by 'zero or more
whitespaces' followed by 'end of string' then print it to EFILE."

Your second regex SHOULD be:

  if ($editbl !~ /\s/) { ... }

because the synonym of "only non-whitespaces" is "not a single
whitespace".  Actually, you need to be slightly more specific:

  if (length($editbl) and $editbl !~ /\s/) { ... }

The problem with the logic of the second regex is that

  "foo bar" =~ /^\S+$/

fails, while

  "foo bar" !~ /^\s*$/

succeeds, since "foo bar" is certainly not made up solely of whitespace
characters.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About