in reply to Why would one want in a regex a class with only a single entry?

I can think of two cases: first, a character class might contain a variable with as few as one character in it (/[$foo]/); second, under the /x modifier, it's a common way to indicate a literal space (/ foo [ ] bar /x).

Replies are listed 'Best First'.
Re^2: Why would one want in a regex a class with only a single entry?
by graff (Chancellor) on Mar 25, 2008 at 22:29 UTC
    first, a character class might contain a variable with as few as one character in it (/[$foo]/);

    ... and in that case, it's worthwhile to test the variable first, to make sure it contains something (i.e. length($foo)>0), because an empty string in that position is a runtime error, if I'm not mistaken.

Re^2: Why would one want in a regex a class with only a single entry?
by ack (Deacon) on Mar 26, 2008 at 03:41 UTC

    Thanks, those both make sense...especially the second one. I don't use the /x modifier much (though as I'm learning, it really helps me remember why I constructed regex's a particular way after I've been away from it for a while) so that one I completely missed.

    I'm curious about the first case, though. Given what I've been led to believe are the penalities of using classes, why wouldn't one check the variable to see if it has just one entry and cast it differently...i.e., in a more regex-engine-efficient form?

    Just curious.

    ack Albuquerque, NM
      Given what I've been led to believe are the penalities of using classes, why wouldn't one check the variable to see if it has just one entry and cast it differently...i.e., in a more regex-engine-efficient form?
      Because only in very unusual cases would the penalties amount to anything noticeable. And the extra code to treat one character specially means more opportunities for bugs as well as less readable/maintainable code.

      In any case, the penalty is gone in 5.10.0 and will be gone in 5.8.9.

        Good point. I suspected that was probably the case.

        Thanks, ysth.

        ack Albuquerque, NM