Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Another RegExp question

by davis (Vicar)
on Aug 17, 2006 at 11:20 UTC ( [id://567888]=note: print w/replies, xml ) Need Help??


in reply to Another RegExp question

The regex /\d{3}/ matches 3 digits anywhere in the string. If you want to restrict it to only 3 digits, you'd need to anchor it (specify either (both in this case) "start of the string" and the "end of the string" — these are logical atomic units):
if ($step =~ /^\d{3}$/) {

Update: YAPE::Regex::Explain gives the following explanation:

The regular expression: (?-imsx:^\d{3}$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \d{3} digits (0-9) (3 times) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

davis
Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
Edited: Fixed slightly mixed tense.

Replies are listed 'Best First'.
Re^2: Another RegExp question
by Ronnie (Scribe) on Aug 17, 2006 at 11:32 UTC
    Thanks for that - guess who's feeling a tad foolish at the moment?!
      Don't - it is a common mistake.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-19 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found