Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: •Re:^2 •Re: Checking for single digit

by Enlil (Parson)
on May 06, 2003 at 04:28 UTC ( #255799=note: print w/replies, xml ) Need Help??

in reply to •Re: Re: •Re: Checking for single digit
in thread Checking for single digit

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. :)


Replies are listed 'Best First'.
Re: Re: •Re:^2 •Re: Checking for single digit
by pfaut (Priest) on May 06, 2003 at 04:52 UTC

    I checked because I can never remember the difference myself. From the camel, 3rd ed., page 179:

    The \A assertion matches only at the beginning of the string, no matter what. However, the ^ assertion is the traditional beginning-of-line assertion as well as a beginning-of-string assertion. Therefore, if the pattern uses the /m modifier and the string has embedded newlines, ^ also matches anywhere inside the string immediately following a newline character.

    So, without the /m modifier, ^ and \A are identical.

    For completeness (same page):

    The \z metasymbol matches at the end of the string, no matter what's inside. \Z matches right before the newline at the end of the string if there is a newline, or at the end if there isn't. The $ metacharacter usually means the same as \Z. However, if the /m modifier was specified and the string has embedded newlines, then $ can also match anywhere inside the string right in front of a newline.
    90% of every Perl application is already written.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255799]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-03-24 20:51 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.