Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

•Re: Checking for single digit

by merlyn (Sage)
on May 06, 2003 at 02:03 UTC ( #255785=note: print w/replies, xml ) Need Help??

in reply to Checking for single digit

Hmm. A lot of answers in this thread give something like /^\d$/, but that's wrong, because that also matches things like "1\n".

You need to use /\A\d\z/, which forces beginning of string and end of string, not $ which means end of string, or just before a newline at the end of string.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Checking for single digit
by TVSET (Chaplain) on May 06, 2003 at 02:08 UTC
    ...but that's wrong...

    There is no such thing :)

    Considering that most peopel don't throw binary data into their input and that chomping is done, /^\d$/ will work properly. And it is way more readable/familiar that /\A\d\z/. But you have a point.:)

    Leonid Mamtchenkov aka TVSET

      and that chomping is done,
      Chomping wasn't done here. And none of the other answers (that I saw) mentioned the restrictions on their match.

      Hence, I must point it out.

      Cargo cult is when you use something you don't fully understand, and then pass it on, without understanding or describing its limitations. This was a perfect example of at least three people cargo-culting the answer.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Actually, considering chomping was not done, the original answer, /^\d$/ would be correct, unlike your answer, which would never match, as there would always be the trailing newline.

        The fact that $ can match just before a trailing newline makes cases like this "do what I mean".


        rather than continuing to cargo-cult code. I have a question refering to your answer (i.e. /\A\d\z/), that being, when there are no modifiers on the regular expression what is the difference between \A and ^? (i have read perlre and gone through MRE, but want to make sure I am not missing any subtlies).

        While I agree that your answer is better than mine in some respects, the OP did not have a chomp statement, and I assumed perhaps incorrectly that someone would type in a number and thus the newline character would go along with whatever number (or other input) the user typed in and not being chomped out (or removed by any other method), then nothing would ever match with \z (so I am at fault for making so many assumptions and not clarifying more fully). I think at least a couple of the answers (including mine) do mention the fact that $ matches the end of the line or right before the newline character at the end of the line.

        But as I mentioned before, I do agree with you, if all you want in a string is to insure that the string is only one digit without anything else in the string (and your using a regex for the checking), then /\A\d\z/ is the correct way to go about it. :)


        Wouldn't this be more versatile (chomp or no chomp)?

        while(defined(my $line = <STDIN>)){ print $1,"\n" if( $line =~ m/\W?(\d)\W?/ ); }

        Update As Enlil pointed out, {1} is implied by \d... I guess I more or less meant it to color the fact it was only one digit hungry.

        YA update Ok, i will try to actually read before posting next time. As Cody Pendant pointed out, the intent seems to be only these characters, not CONTAINS these characters.

        "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
      I actually like the \A and \z from an aesthetic standpoint. To me they just look much better than ^ and $. Of course, I'll still use whichever one best fits the situation I'm dealing with.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255785]
[marto]: I'm all for that
[choroba]: Trainspotting without OV is just a half of the movie!
LanX just single handedly stopped Brexit!

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-02-19 10:51 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (262 votes). Check out past polls.