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

Redundant regex ([0-9]{2} | [0-9] {2, 4})?

by Popcorn Dave (Abbot)
on Feb 26, 2009 at 22:35 UTC ( #746692=perlquestion: print w/replies, xml ) Need Help??
Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I was surfing around Yahoo Answers the other day and a person was asking for an explanation of what the following regex did:

s/^\[[0-9]{2}-[0-9]-{2}-([0-9]{2} | [0-9] {2, 4})\]\s+//;

Of course I pointed him towards YAPE::Regex and presented him with the explanation output, but what struck me as odd was the

([0-9]{2} | [0-9] {2, 4})

Unless I'm mistaken, you're matching 2 numbers or between 2 and 4 numbers. Why would use the | there? If you're matching between 2 and 4 numbers you're matching 2 already aren't you? Am I just missing something here?

To disagree, one doesn't have to be disagreeable - Barry Goldwater

Replies are listed 'Best First'.
Re: Redundant regex ([0-9]{2} | [0-9] {2, 4})?
by ikegami (Pope) on Feb 26, 2009 at 22:42 UTC

    No, /[0-9]{2} / does not match a subset of what / [0-9] {2, 4}/ matches.

    /[0-9]{2} / only matches "99\x{20}", where 9 can by any digit 0-9
    / [0-9] {2, 4}/ only matches "\x{20}9\x{20}{2,\x{20}4}", where 9 can by any digit 0-9

    Update: Fixed copy and paste bug in in second item.

      Holy crap, I need glasses! I completely missed the space in there and didn't realize it until I increased the font size.

      That makes perfect sense now. Duh.

      To disagree, one doesn't have to be disagreeable - Barry Goldwater

        Spaces removed, /[0-9]{2}/ does match a subset of what /[0-9]{2,4}/ matches.

        I'm sure what he wanted was just /[0-9]{2,4}/.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://746692]
Approved by Corion
[karlgoethebier]: "that blue pain" == Salzglasur as far as i remember
[karlgoethebier]: Paint!
[shmem]: karlgoethebier the blue pain comes after all bembels are empty
[karlgoethebier]: Confusing "Bembel" with "Membel" might result in a slap in the face in Hesse...
[karlgoethebier]: shmem: Yes. Greetings from Dr. Freud
[Discipulus]: blue pain is a synōnymía

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-05-24 09:00 GMT
Find Nodes?
    Voting Booth?