Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I know that the first time I was learning about regular expressions, "do ___" was much less helpful to me if it didn't include an explanation of why it should be done. I am including one here for completeness.

The following code

print "good input\n" if /^\d{5}$/;

Should Could be rewritten:

print "good input\n" if /^\d{5,5}$/;

This is because {}s can contain 2 numbers seperated by a comma. The number on the left is the minimum number of the charachters (or things). The number on the right is the maximum number of charachters (or things). The charachter or thing is whatever is to the left of the {}s. So, \d{5,5} says 5 digits and only 5 digits, whereas \d{5} says at least 5 digits -- but possibly more. (Note that \d means a charachter that represents a number -- i.e. a digit)

Of course, the reason the original regular expression will still work is the ^ and $ charachter mean "beginning of a line" and "end of a line", respectively. So my regular expression says "at least 5 digits but not more then 5 digits which start on the beginning of a line and end at the end of a line" while the original regular expression says "At least 5 digits between the beginning and end of a line -- and possibly more".

Of course, the thing we have to pay attention to here is that mine would fail if we tried to match the string "Foo\n12345\nBar" This is because it fits all the constraints of the regular expression. However, because of the angle brackets <> -- which reads in a single line at a time, you know you can only have one line.

Update: Thanks for pointing out my mistake Tom


Want to support the EFF and FSF by buying cool stuff? Click here.

In reply to Re: Re: compare stdin for a number by Vautrin
in thread compare stdin for a number by anoxer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found