Not everything is a regex

petdance on 2005-02-22T16:12:43

We love the regexes in Perl, but they don't always make sense. If you're doing

if ( $str =~ /^foo$/i )
then write it as
if ( lc $str eq "foo" )


split()

Dom2 on 2005-02-22T16:33:08

My pet peeve is the opposite: people who use a string as the first argument to split(). Not only is it intended to be a regex, but some strings have special behaviour (" " for instance).

-Dom

'$' ne "\\z"

rafael on 2005-02-22T17:27:49

Actually that's lc $str eq "foo" or lc $str eq "foo\n".

Re:'$' ne "\\z"

petdance on 2005-02-22T17:32:27

Except that that distinction is irrelevant in this case. It's just trying to match a value coming out of a hash. But point taken.

A regex question then?

GAVollink on 2005-02-22T22:16:37

Is there a fast way to force a variable within regex tags to not follow regex escapes?
$filesearch = "Financials [1994]";

foreach ... {
  ...
  if ( $file =~ m/$filesearch/ ) ...
I'm currently doing this first:
$filesearch =~ s/([][])/\\$1/g;
$filesearch =~ s/([()])/\\$1/g;
But it seems so messy.

Re:A regex question then?

petdance on 2005-02-22T22:23:47

Look at the quotemeta() function. Also the \Q metacharacter.

Except sometimes.

Aristotle on 2005-02-23T14:09:04

When the string is in $_, I'll often use a regex for a string comparison so I can avoid spelling out the variable. This is particularly often the case when grep is involved.

It still annoys me though.

And abuse of regexen for string comparisons tends to crop up a lot in badly written scripts by people who don't really know Perl well (such as most of the stuff that circulates on “free CGI scripts!” sites.)

Not with unicode...

demerphq on 2005-03-22T17:37:34

The trouble is that this type of advice is incorrect in the modern unicode world.

I think youll find that the equivelency you post doesnt hold for certain unicode characters like the greek sigma chars. Lowercase/Uppercase arent safe forms to do case insensitive comparisons in unicode instead they have a special form called "foldcased" which is intended for this purpose and involves prenormalizing the string.

Anyway, sorry this is with my pedant hat on.