55
Capture,Reuse
86
Apples
Oracles
Oranges
Ore
Themostimportantletteristheleft-most one:
A
comesbefore
O
inthealphabet,so allwordsthat
beginwith
A
willcomebeforeanywordthatstartswith
O
.Butwhatifthewordsstartwiththesame
letter? Thenyoucomparethesecondletters,andsoforth.
Thisis thesamewithdates. In the American human-readable format,
03-09-2001
wouldcome
before
05-22-1978
,as
03
comesearlierinthenumericalscalethandoes
05
.Butchronologically,of
course,
05-22-1978
isseveraldecadesbefore
03-09-2001
.Intheprogrammer-friendlyversion,the
“biggest”partsofthedatecomefirst,i.e. theyear,thenthemonthandday. Thismakessortingeasy,
becausethealphabetical-orderofthedatetextisthesameasthechronologicalorderoftheactual
dates.
So, given the human readable version of the dates, write a regex that converts them to the
programmerfriendlyversion.
Answer
Find (\d+)-(\d+)-(\d+)
Replace $3-$1-$2
Exercise: Zero-padthedates
Theaboveexerciseisn’tcompletelyrealistic,however,becausethehuman-readabledatesareusually
writteninamoreshorthandway:
3-9-2001
11-14-2012
5-22-1978
ThemonthofMarch,inotherwords,iswrittensimplyas
3
,notas
03
Whyisthatextrazerothere? Ifyouthinkofthecomputer-sortablescenario,thoseextrazerosare
vital. Considertheissueoftwodateswiththesameyear. Inhumanreadableformat:
11-20-2002
3-20-2002
Inmachine-readableformat: