in reply to Regexes Are Not For String Comparison

I've done this sort of thing lots of times before myself (using \Q\E or quotemeta(), at least). Reading your post, I've been trying to figure out why, and I've reached this conclusion: When your most impressive tools are regexes, everything looks like a pattern.

In the big cognitive precedence tables stored in most Perl users'(*) wetware, m// ranks above lc(). So writing this kind of code is understandable. Plus, lc() is usually presented (or else, can get pigeonholed after learning about it) as a tool for changing a string, so someone trying this sort of comparison might think "I need to compare two things, not change the identity of one". So using lc() with eq might not even come up for consideration.

I'm just meditating on the reasons why we pick certain coding forms. We all know TMTOWTDI, but how do people come to choose their ways?

-- Frag.

(*)IMHO, this isn't just me.

  • Comment on Re: Regexes Are Not For String Comparison

Replies are listed 'Best First'.
Re: Re: Regexes Are Not For String Comparison
by japhy (Canon) on Apr 24, 2001 at 20:29 UTC
    But I find that the simpler tools are rarely used, while the more complex ones are used oft and poorly. Many people see regexes, and never understand them; but that does not stop them from using them. And the more regexes get used by those who don't understand them, the more they seem to be mysterious giants.

    I see people that put /(.*)/ in their code for no reason. It's just waiting for data to come along and break it.

    japhy -- Perl and Regex Hacker

      I'm not disagreeing (I think), but I'm trying to get at why people reach for the more complex ones. Using m// isn't by itself complex and is usually one of the first Way Cool Perl features taught to newbies, and people can easily think that they understand the basic regex components (like $1 or /i or $^ or .*) that they've read about or seen used in tutorials. If you're saying that people use it just to make themselves look 'leet, I don't want to discount that, but it's more than just that -- there are reasons for thinking and choosing certain things, that has to do with the way the language is structured, or in how the language is learned.

      -- Frag.

      P.S. I'm confused on the problem with /(.*)/ -- do you mean /"(.*)"/? How can copying an entire string into $1 break? (Although it is certainly dumb.)

        By asking how /(.*)/ can break, you're showing that you don't know that . can't match a newline, unless you use the /s modifier to a regex.

        And, they're doing things silly like

        if ($string =~ /.*/) { ... }
        or
        if ($string =~ /.*\.html/) { ... }
        Those show they don't know what they're doing. It's like seeing a hammer being used to pound a nail into wood, and then going around slamming a hammer into everything you see.

        japhy -- Perl and Regex Hacker