TextMate, Regex & America

Supercharge TextMate’s Find & Replace using Regular Expressions. 

I was recently working on a website when my american client reminded me that he prefers to write his dates backwards. As you know: 

24-08-2010  (Day, Month, Year)

is the sensible way to format a date while: 

08-24-2010  (Month, Day, Year)

is the standard in the US.

Find & Replace using Regular Expressions

Faced with having to edit ~40 pages just to swap two numbers around, the lightbulb appeared above my head and I started reading the TextMate Manual on Regular Expressions.

By using some round brackets in my search, I create Capturing Groups and then reference these in my replacement string using $1, $2, $3 etc. 

Find

date: (\d\d)-(\d\d)-(\d\d\d\d)

Replace with

date: $2-$1-$3

As you can guess, the replacement will swap the 1st and 2nd group around. My 10 minute problem was fixed in seconds. 

These groups and back-references can do some powerful things, I’ll definitely be reading up on them soon. 

  1. leanminded posted this
Short URL for this post: http://tmblr.co/ZUsv9y_8yGi
blog comments powered by Disqus