Skip to content

Regex, The (2008)

So I have this list of movies I’ve seen - it’s a plain text file accumulated from roughly the steps outlined here - let’s say I just saw the movie “Cloverfield” on DVD…

  1. Fire up Opera
  2. Type g imdb cloverfield
  3. Click the first link in the results to go to the IMDb entry for that movie (to verify that it’s indeed the one I saw)
  4. Select the movie’s title on that page
  5. Hit ⌃ ⌥ SPACE to open selected text in Quicksilver
  6. Type AP to get the command “Append to file…”
  7. Hit TAB and hold F (for “filmlisten.txt”)

That’s it — the movie has been added to my list.

The problem is: At some point in time between the inauguration of my sacred file and now, they (IMDb) changed the format of the title for movies starting with “The …”. You see, before (back in the day, once upon a time, etc.) — Kubrick’s 1980 movie was listed as Shining, The (1980) whereas now, it’s listed as The Shining (1980).

Being so darn picky about that, my textfile was a mess… you see, I like a certain kind of order in the universe (at least in my part of it) so once in while, I’d follow that last step with an additional ⌃ SPACE + ↩ to open the textfile in TextMate, then F5 to sort the lines alphabetically.

But alphabetically just wasn’t good enough anymore, because some movies we’re listed in the old format while others were in the new one. And there were just about a couple of hundred titles pending a fix so I needed a command of some sort — and no, doing a simple Find & Replace wouldn’t be able to fix this.

Enter Regex — Regular Expression(s)

A Regular Expression is the perfect solution here, because you can easily match something, store it and then come back and fetch it again. TextMate lets you do this in the Find & Replace dialog, so I hammered this in:

Regular Expression: ^(.*)(,) (The)( \(\d{4}\))\s*$ - Replacement: $3 $1$4
Regular Expression: `^(.*)(,) (The)( \(\d{4}\))\s*$` - Replacement: `$3 $1$4`

- and there you have it. All titles properly titled and order has once again been restored to The Galaxy.