Yes, it's a stupid example. It illustrates, though, how easy it is to
work with regexes. There's a lot of shortcuts like that. I think
that's pretty nice. You can write short code which is easy to read.
You can -- you're not forced to.
Roughly equivalent code in Python 3:
1 import re
2 foo = "2012-07-13"
3 m = re.match(r'(....)-(..)-(..)', foo)
4 if m is not None:
5 print(m.group(1), m.group(2), m.group(3))
This is worse in every aspect. It's harder to *write* and harder to
*read*. Well, whatever. I'm not forced to use Python for everything.
On the other hand, Perl can be very confusing. Just search the web for
"perl pitfalls". Or think of the sigils, different contexts for
variables and what not. That's just a little bit too much (or maybe
I'm simply not yet used to it -- shell scripts aren't that better).
Enter Ruby. I never had a closer look at Ruby because I thought of it
as "a modern version of Perl" -- and I didn't like Perl at that time.
But! It appears to be true. Ruby eliminates a lot of Perl's annoyances
while keeping the candy.
1 foo = "2012-07-13"
2 if foo =~ /(....)-(..)-(..)/
3 puts "$1, $2, $3"
4 end
Isn't that nice?
____________________
time_t party!
At 11:01:20 UTC on July 13, 2012, the Unix time number will reach
0x50000000 (1,342,177,280 seconds).