Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Take out the date using Regex.

by muba (Priest)
on Jan 05, 2013 at 11:04 UTC ( #1011778=note: print w/ replies, xml ) Need Help??


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>


Comment on Re: Take out the date using Regex.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-09-17 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (98 votes), past polls