Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Another RegExp question

by davis (Vicar)
on Aug 17, 2006 at 11:20 UTC ( #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.


Comment on Re: Another RegExp question
Select or Download Code
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
Node Status?
node history
Node Type: note [id://567888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2015-07-06 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (72 votes), past polls