I have this problem with regular expressions. They’re too handy for their own good.Â You say you don’t know what a regular expression is?Â Well, let me just tell you, you don’t know what you’re missing out on.Â Think of it like a really super-duper complicated way of searching for something real specific when it’s slapped in the middle of a whole back of other crap you don’t want to even mess with.Â But it’s not even just searching for something, you can search and replace really complicated text that’s just too big to do by hand.
More than that, it’s not like you just want to find every reference to the name “Meg” on your computer and change it to “Poop” … you can use regular expressions to transform Huge Text File (A) into just theÂ little sub-sections of important stuff … or you can convert (A) from one textual format to another, like from HTML to Whiskey, or whatever the hell new-fangled nonsense is going on out there in web junky land.
There are plenty of programs out there that make use of regular expressions and you don’t even know about it.Â I first started playing around with them way back in 1995 or so, not long after Rich Siegel started selling BBEdit for Macintosh, and I’ve been using them since.Â Here’s an example … say you have a text file that looks like this:
Jimmy Jack Johnson sells 43 seashells to Yo Momma. She paid about three-fiddy.
You can do a regular expression find/replace on that text, which would look something like this:
's/^(J.*)\ J.*\ (J.*)\ (s)...s\ ([0-9]+).*\r/\1\ sucks\ \4\ \2\3\.\r/g'
Which would now make that first text look something like this:
Jimmy sucks 43 Johnsons.
This sort of thing would be great for pranking your frienemies, but I have yet to hear of such a thing catching on.Â Which brings us to my current dilemma.Â I’ve come to rely on regular expressions so much that I believe they can do anything.Â Unfortunately, I think I’m demanding too much, or at least my computer is unwilling to give me everything it’s got in order to accomplish this task.Â I have aÂ text file, freshly spit out from Excel, that has 43 columns of textual data over 160 some-odd rows … not that big of a file, it’s only got about 43,000 characters.Â It’s tab-delimited, meaning that in the text file, there’s a unprinted tab character (\t) between each section of the data, to separate out each of the cells.Â What I wanted to do was churn this tabbed text file into an XML .plist, or Property List file.Â I’m sure there’s a more elegant way of doing this (without having to do it by hand, obviously), but I chose to create a regular expression … here’s what the “Search For” string looks like:
And here’s what the “Replace With” string looked like:
Needless to say, it did not go over well.Â BBEdit died after ten minutes of churning.Â Perl sucked up 2 Gigs of RAM before I had to kill it.Â Sure I could process the file little bits at a time, but wouldn’t that be taking the fun out of regular expressions?
EDIT / Update:Â
Yeah, about all that. With a tiny bit of touching up, a demo version ofÂ TextMateÂ processed the whole file in about 6 seconds. Sounds like someoneâ€™s going to actually have to pay for a software registration when the demo expires. Nothing like knocking my socks off and shaming a few industry leaders to earn your keep. Nice job there buddy.