Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Take out the date using Regex.

by Anonymous Monk
on Jan 05, 2013 at 10:47 UTC ( #1011776=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a simple question for you. I have a string like the below.

$string = "Sat, 05 Jan 2013 04:00:15 GMT";

I want to take out the "05 Jan 2013" from that using regex.

Is that possible?. ie, i want to cut the "Sat," & "04:00:15 GMT" .

pls Note that this is common "Sat" could be "Mon" or "Sun" or "Fri" and "04:15 GMT" could be any time. Pls help monks.

Thx

Comment on Take out the date using Regex.
Download Code
Re: Take out the date using Regex.
by muba (Priest) on Jan 05, 2013 at 11:04 UTC

    I'm not quite sure what you mean by "take out" - is that a "take out and ditch", or a "take out and keep around for later use"?

    # Ditch: my $string = "Sat, 05 Jan 2013 04:00:15 GMT"; $string =~ s/\d{2} [A-Z][a-z]{2} \d+ //; print "Take out and ditch: $string\n"; # Output: Take out and ditch: Sat, 04:00:15 GMT
    # Keep around: my $string = "Sat, 05 Jan 2013 04:00:15 GMT"; my ($match) = $string =~ m/(\d{2} [A-Z][a-z]{2} \d+)/; print "Take out and keep: <$match> found in <$string>\n"; # Output: Take out and keep: <05 Jan 2013> found in <Sat, 05 Jan 2013 +04:00:15 GMT>

    Update: come to think of it, there's always more than one way to do it, and in this case regexes aren't even needed. The same result is as easily (and much more efficiently) achieved using substr:

    # Ditch: my $string = "Sat, 05 Jan 2013 04:00:15 GMT"; substr($string, 5, 12, ""); print "Take out and ditch: $string\n"; # Output: Take out and ditch: Sat, 04:00:15 GMT
    # Keep around: my $string = "Sat, 05 Jan 2013 04:00:15 GMT"; my $match = substr($string, 5, 11); print "Take out and keep: <$match> found in <$string>\n"; # Output: Take out and keep: <05 Jan 2013> found in <Sat, 05 Jan 2013 +04:00:15 GMT>
Re: Take out the date using Regex.
by Anonymous Monk on Jan 05, 2013 at 11:06 UTC

    Yes its possible, but don't forget about strftime/DateTime and the like

    FWIW, this is basic regex, perlintro/perlrequick stuff :)
    s/,............/, /s;

    The advanced regex
    s/...,.\K...........//s;
    s/(?<=...,.)...........//s;

Re: Take out the date using Regex.
by karlgoethebier (Curate) on Jan 05, 2013 at 11:25 UTC

    Like this...

    #!/usr/bin/perl use strict; use warnings; my $date_time = qq(Sat, 05 Jan 2013 04:00:15 GMT); $date_time =~ m/(.+), (\d{2} \w{3} \d{4}) (\d{2}:\d{2}:\d{2}) (\w+)/; for ( $1, $2, $3, $4 ) { print qq($_\n) } __END__ Karls-Mac-mini:monks karl$ ./dt.pl Sat 05 Jan 2013 04:00:15 GMT
    Update: Edit typo...
    $date_time =~ m/(.+), (.+) (.+) (.+)/;

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Take out the date using Regex.
by CountZero (Bishop) on Jan 05, 2013 at 13:16 UTC
    Or using split:
    use Modern::Perl; my $string = 'Sat, 05 Jan 2013 04:00:15 GMT'; my $date = join ' ', (split /\s/, $string)[1 .. 3]; say $date;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-10-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls