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

Re: match + regexp synonyms?

Thread Previous
Jeff 'japhy' Pinyan
January 30, 2002 14:07
Re: match + regexp synonyms?
Message ID:
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

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About