Regular Expressions: Not Just for E-Mail Validation
Regular expressions are a very powerful, but very confusing tool. It takes a long time to figure out the grammar, and even today just looking at a regular expression makes my brain go numb for a moment (but writing them is much easier). However, they’re good for more than just validating e-mail addresses. They can be a powerful everyday tool if you happen to need to reformat some text.
For example, I recently needed some seed data for an application and wanted to get 50 girl and 50 boy names. I did a quick google to find popular names and came to the Top 100 baby names of 2006. Great! I copy and pasted the names into TextMate and I was left with something that looked like this:
1 Emma
2 Madison
3 Ava
4 Emily
5 Isabella
6 Kaitlyn
7 Sophia
8 Olivia
9 Abigail
...
Now, that’s pretty simple, but I wanted it in a form like this (Ruby array shorthand):
%w(Emma Madison Ava Emily Isabella Kaitlyn Sophia Olivia Abigail...)
Rather than either retype or manually delete the numbers and proceeding tabs, I just wrote the following find and replace in TextMate (make sure you check the Regular Expression option):
Find: ^[0-9]+\t([A-Za-z]+)\n
Replace: $1
That is, find anything that has a line starting with a series of 0-9, followed by a tab, followed by some letters followed by a line break, and replace it with just the letters and a space. This leaves us with the following:
Emma Madison Ava Emily Isabella Kaitlyn Sophia Olivia Abigail...
Which is almost exactly what I wanted to begin with. Now I’ve saved myself a little bit of time and a lot of repetitive work. You’d be surprised how often using regular expressions to reformat some copy and pasted text comes in handy.
Leave a Reply