Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Fetching date

by Anonymous Monk
on Jan 21, 2004 at 15:56 UTC ( #322914=perlquestion: print w/replies, xml ) Need Help??

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

I need to fetch all data with this syntax such as:
Jan 21 00:41
To fetch this in a regular expression I tried: $datehit =~ /(\w+\s\d{2}\s\d{2}\:\d{2}
Is this correct?

Replies are listed 'Best First'.
Re: Fetching date
by ysth (Canon) on Jan 21, 2004 at 16:05 UTC
    Other than the missing )/, looks like it.

    Do you know for sure that the day-of-month will always be given with 2 digits, and not as e.g. "Feb 1"?

      thanks all, but it could also be: Feb 1 00:41 So I could use all your examples for the above also?
        Anonymous Monk,
        You could just make the \d greedy and say \d+ but that runs the risk of mistaken garbage for treasure.
        my ($mon, $day, $hour, $min) = $date =~ /(\w+)\s(\d\d?)\s(\d\d):(\d\d) +/;
        The above should be closer to what you want, but you should keep some things in mind. If you know that the month abbreviation will always be 3 letters long then say so. When the day is 1 instead of 12 does the number of spaces between the hour change in order to get the fields to line up (perhaps in a log)?
        my ($mon, $day, $hour, $min) = $date =~ /(\w{3})\s+(\d\d?)\s+(\d\d):(\ +d\d)/;
        Cheers - L~R
Re: Fetching date
by tcf22 (Priest) on Jan 21, 2004 at 16:11 UTC
    Do you want to capture the information like this:
    my $date = 'Jan 21 00:41'; my ($mon, $day, $hour, $min) = $date =~ /(\w+)\s(\d+)\s(\d+):(\d+)/; foreach($mon, $day, $hour, $min){ print "$_\n"; }
    or do you just want to match it, in which case this should work
    $datehit =~ /(\w+\s\d{2}\s\d{2}\:\d{2})/;

    - Tom

Re: Fetching date
by dragonchild (Archbishop) on Jan 21, 2004 at 16:18 UTC
    Have you tried it? Does it work the way you expect it to? Your regex (assuming you have the trailing slash) looks like it will grab what you're looking for, assuming the string contained in $datehit has that match ...

    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Fetching date
by davemabe (Monk) on Jan 21, 2004 at 16:39 UTC
    If performance is going to be an issue, I would suggest using the much more efficient substr function. Of course, depending on the situation the difference may be neglible, but I've worked on projects where performance was a big issue and this spared me a significant amount of cycles.

      I am not sure I agree with you. The AM wants to fetch all data in a specific format. The substr function extracts a substring. Perhaps you were thinking of index? While I agree that index can often be faster than a regex when detecting the precence of a string within a larger string, it requires an exact substring and is not maleable as say the \w character class in a regex. That type of flexibility seems to be required to solve this problem.

      Cheers - L~R

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-10-24 08:59 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.