Regular Expressions: Not Just for E-Mail Validation

Posted by on February 14th, 2008.

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.

Share:

Comment on this post (0 comments)


Improved BetterNestedSet Plugin

Posted by on October 27th, 2007.

On a recent project when I was using the BetterNestedSet plugin to manage a large hierarchal set of data, I encountered a problem that required me to find all of the items in a nested set that had children and those that didn't. In nested set terms I wanted: all 'parent' nodes and all 'leaf' nodes that exist within the 'tree'.

If you want to do this through the current BetterNestedSet interface you might be tempted to do something like this:


Continue reading this post

Share:

Comment on this post (1 comment)


DRY Internal and External Notifications using ActionMailer

Posted by on August 1st, 2007.

I know that most Rails developers have come across the problem of notifying people by email that they have an internal message on your app’s own messaging system. Usually this is easy enough, and elegantly done in your model by using

However, let’s consider a situation where you need duplication of data on two different processes. For example, if you would like to generate a notification message when some posts to a common forum. The quick way to do this would be by calling two separate methods in your controller, like so:

This requires you to generate the subject and the body of the message and use the methods to work out the logic.

However, with a little ActionMailer hackery, you can make this scheme much cleaner.

In your model,

Next, in views/my_mailer/new_posting.rhtml

The following three lines generate a new MyMailer object, create the email specified by the method (in this case, :notify_posting) using ActionMailer’s create! method (which in turn uses Ruby’s glorious send method), and pull out the TMail object that ActionMailer sends to the SMTP server. Conveniently, the TMail object lets you extract out the subject and body, which lets you map them to your InternalMessage class.

The above code generates the email, pulls out the generated subject and the body and uses it as the subject and body of the internal message. The idea is that the content of both the email notification and the internal email match up. This method does not require you to setup your subject and body. Instead, this method lets you use ActionMailer as a rendering framework for generating text/html for all of your internal messages. DRY is Good!

Share:

Comment on this post (0 comments)