Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

simple regexp question

by jeanluca (Deacon)
on Feb 10, 2006 at 13:02 UTC ( #529347=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I need to parse dates. For example the following to dates are possible:
2005-100 2005100
So the only difference is that there might be an - or not. I'm not sure how to create an 'or' in a regexp, so what should my regexp look like ?

Thanks a lot

Replies are listed 'Best First'.
Re: simple regexp question
by osunderdog (Deacon) on Feb 10, 2006 at 13:05 UTC

    Use ? To indicate a character that can appear zero or one time.

    For example:

    $ perl -e "print '2000-20-30' =~ /\d{4}-?\d{2}-?\d{2}/" 1 $ perl -e "print '20002030' =~ /\d{4}-?\d{2}-?\d{2}/" 1

    Hazah! I'm Employed!

Re: simple regexp question
by davorg (Chancellor) on Feb 10, 2006 at 13:06 UTC
Re: simple regexp question
by thedoe (Monk) on Feb 10, 2006 at 15:10 UTC

    Instead of using a regex to parse your dates, why not use one of the pre-built modules in CPAN for date parsing?

    Date::Parse is the first that comes to mind. It is simple to use and will parse just about any common date format. This means that even if the date input changes, your code probably won't have to.

Re: simple regexp question
by misterb101 (Sexton) on Feb 10, 2006 at 14:59 UTC
    what exactly is 2005100?
    2005-1-00 ?
    2005-10-0 ?
    if you don't know how many chars a month/day can have you can never create a clever enough regexp to my humble opinion


      It could be that his dates are 4 digit year followed by 3 digit julian day; no one said anything about month/day.

        Mixing the Gregorian (2005) and Julian (001) calendar? is that very likely?
Re: simple regexp question
by grinder (Bishop) on Feb 10, 2006 at 17:21 UTC

    Maybe I'm missing something really obvious, but if the - (dash) is optional, then mark it such in the regular expression...

    my ($year, $num) = ($_ =~ /^(\d{4})-?(\d+)$/);

    This way you will be guaranteed of getting both parts, regardless of whether a dash is present or not.

    • another intruder with the mooring in the heart of the Perl

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2023-06-03 00:19 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (6 votes). Check out past polls.