Regex E-Mail String Matching

Recently, I was playing about with Keyboard Maestro – a powerful (Mac) tool to automate tasks that you have to do on a regular basis. It seems daunting at first, but has proved to be pretty useful.

One of the the things I wanted to achieve was to be able to strip out a whole pile of data to return just the e-mail addresses, rather than have to go through them manually.

In order to do that, I needed a Regex (regular expression) pattern that would match e-mail addresses, with all of the weird formats they take: multiple TLDs, periods and dashes in the alias… and not to forget the plus sign that lets you create additional handles in GMail.

I looked around the web but didn’t find any patterns that did what I wanted. There were a few, but most seemed based on e-mail validation rather than filtering the address out of a bigger data set. So, I created my own:

\b([A-Za-z0-9%+._-])+[@]+([%+a-z0-9A-Z.-]*)\b

I’ve been using it for a couple of weeks and haven’t come across any addresses that it hasn’t picked up correctly yet. If you spot any, give me a shout in the comments below.

You can test out the expression for yourself over on the incredibly useful site regexr.com.

Leave a Reply