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


in reply to Take out the date using Regex.

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>