Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011778]
help
Chatterbox?
Discipulus a missing semicolon after use LWP::UserAgent caused very strange behaviour and not the normal warning...
[choroba]: Yes, sometimes missing ; after the use clause just interprets the next line somehow and passes the result to the used module which could happily ignore it. Been there, seen that.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-02-28 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (398 votes). Check out past polls.