in reply to how to state this by REL

While the guy who gave you this regex obviously knows what he's doing, and admittedly, it does function according to spec, I'd still change it.

The simple reason being I had to lookup what \A and \z do. Far more common anchors for matching the beginning and end of strings are ^ and $. Even the documentation describes them as "The two most common anchors".

so I'd change it to

$name =~ /^[a-zA-Z][\w.-]*$/
I might even be tempted to explicitly escape that '.', even though it's not strictly necessary.
$name =~ /^[a-zA-Z][\w\.-]*$/
To me, that makes it more obvious that we're matching a dot.

I wonder what percentage of 'average ability' Perl programmers could tell you the subtle difference between \z and \Z without reference to a manual?

---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: how to state this by REL
by herveus (Prior) on Nov 09, 2005 at 13:43 UTC
    Howdy!

    I wouldn't change that RE. Since you had to look up \A and \z, did you also look up ^ and $? They don't do what you say they do. \A and \z do exactly what you attribute to ^ and ?.

    Further, escaping . in a character class is gratuitous.

    To elaborate: ^ matches the beginning of the line, while $ matches the end of the line (unless the line ends with a newline, in which case the anchor matches before the newline. \A is always the start of the string. \z is always the end of the string. ^ and $ depend on whether you are using the /m modifier or not.

    TheDamian makes fair arguments for using \A and \z (along with /x, /m, and /s routinely). The core argument is that doing so makes your regexen behave with less surprising behavior all the time. Consistency, and all that.

    yours,
    Michael