Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Take out the date using Regex.

by muba (Priest)
on Jan 05, 2013 at 11:04 UTC ( [id://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>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found