http://www.perlmonks.org?node_id=1011776

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

Replies are listed 'Best First'.
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
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 karlgoethebier (Abbot) 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 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;