Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: REGEX Match Single Digit

by spickles (Scribe)
on Aug 28, 2009 at 16:00 UTC ( [id://791950]=note: print w/replies, xml ) Need Help??


in reply to Re: REGEX Match Single Digit
in thread REGEX Match Single Digit

kennethk -

*SLAPS forehead*
That simple, eh? That will match just the number 1, and not 11, or 111, etc. Lemme try it out. Does that work if I read the number one from <STDIN> and chomp the newline? Is the digit 1 now string 1?

Replies are listed 'Best First'.
Re^3: REGEX Match Single Digit
by ikegami (Patriarch) on Aug 28, 2009 at 16:11 UTC

    That will match just the number 1, and not 11, or 111, etc.

    It matches "1" and "1\n" only.

    /^1$/:

    1. At the start of the input,
    2. match "1", then
    3. optionally match "\n", then
    4. match the end of the input string.

    /^1\z/ will only match "1".

Re^3: REGEX Match Single Digit
by kennethk (Abbot) on Aug 28, 2009 at 16:21 UTC
    One of the things I love about Perl is the weak typing on scalars. The digit 1 and the string "1" have roughly equivalent internal representations and which one is used depends upon the context of your call. For example the line

    print "1 line of text" + 1;

    will output the string "2". You start with a string and a number combined with the + operator. That operator takes two numbers as arguments, so perl attempts to convert the string to a number - this truncates from the first non-numeric character. We then evaluate 1+1 = 2. That two is then passed to the print subroutine, which takes a string input. Therefore, 2 is converted to "2" and output to STDOUT. Coming from a strongly-typed background, that still fills me with a profound warmth every time I do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://791950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-06-16 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.