Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

State this simple regex in English?

by misterperl (Pilgrim)
on Apr 20, 2016 at 14:37 UTC ( [id://1161001]=perlquestion: print w/replies, xml ) Need Help??

misterperl has asked for the wisdom of the Perl Monks concerning the following question:

I was trying to match
CAT[2-9]*$
CAT1 does NOT match. Which is actually good news because that's exactly what I wanted, but I'm a but surprised because I thought , at least in English, it sounded like a match? In English is this acts like:
"CAT followed by nothing, or else if there ARE chars after CAT, all must be 2-9 only."
But my English interpretation would be:
"CAT followed by 0 or more digits 2-9."
so CAT1 meets that standard since it is CAT followed by 0 chars that match 2-9?

Can you express this regex in English that makes sense? My *correct* interpretation seems overly complex?

Replies are listed 'Best First'.
Re: State this simple regex in English?
by Athanasius (Archbishop) on Apr 20, 2016 at 14:43 UTC

    Hello misterperl,

    The $ at the end of the regex anchors the match to the end of the line. Since CAT1 does not satisfy CAT followed by zero or more characters in the range 2 to 9 followed immediately by the end of the line, it doesn’t match.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: State this simple regex in English?
by toolic (Bishop) on Apr 20, 2016 at 14:44 UTC

    Is this any better? Tip #9 from the Basic debugging checklist: YAPE::Regex::Explain

    The regular expression: (?-imsx:CAT[2-9]*$) 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): ---------------------------------------------------------------------- CAT 'CAT' ---------------------------------------------------------------------- [2-9]* any character of: '2' to '9' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
      thanks guys,

      STILL seems a bit ambiguous since, afterall, there ARE 0-many 2-9's at the end (there are 0 of them!)... But I suppose it only makes sense to act like this because otherwise it would behave just like /CAT.*/

      YAPE::Regfex::Explain looks cool never tried it or knew about it, thanks for that tip...

        if your regex were "simply" CAT[2-9]* then CAT1 would match, but the $ (which means end of string) in CAT[2-9]*$ prevents a match.
        Would you expect C1AT to match?
        YAPE::Regfex::Explain looks cool ...

        It does, but of course be aware that, per the docs, "There is no support for regular expression syntax added after Perl version 5.6, particularly any constructs added in 5.10."


        Give a man a fish:  <%-{-{-{-<

        You may also be interested in use re 'debug';

Re: State this simple regex in English?
by stevieb (Canon) on Apr 20, 2016 at 14:47 UTC
    "CAT followed by 0 or more digits 2-9."

    That's pretty much it, except: "CAT followed by 0 or more digits between 2 and 9 only, followed immediately by end-of-string", where end of string is the $

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found