Regular Expressions

Posted on July 11, 2007

TextPad® is a powerful, general purpose editor for plain text files. I have used it for years and recommend it highly. One of the best features is that you can do ‘find and replace’ requests using regular expressions.

I found a good set of regular expressions for finding phone numbers in text files. In the interest of saving you from reinventing the wheel, here they are.

The “^” character means that it will find only phone numbers that begin at the first character of the line. If you don’t want that, remove the “^”.

For numbers formatted like ###-###-#### or ###.###.####
^[0-9]\{3\}[-.][0-9]\{3\}[-.][0-9]\{4\}
For numbers formatted like (###)###-#### or (###)###.####
^[(][0-9]\{3\}[)][0-9]\{3\}[-.][0-9]\{4\}
For numbers formatted like (###) ###-####
^[(][0-9]\{3\}[)][[:space:]][0-9]\{3\}[-.][0-9]\{4\}

PsPad is another great text editor. It has an FTP feature that allows you to work on files on a server in real-time. The regular expressions for PsPad are as follows:

For numbers formatted like ###-###-#### or ###.###.####
^[0-9]{3}[-.][0-9]{3}[-.][0-9]{4}
For numbers formatted like (###)###-#### or (###)###.####
^[\(][0-9]{3}[\)][0-9]{3}[-.][0-9]{4}

For what it is worth, TextPad is a better utility if you are trying to clean up text files. PsPad is better if you are working on files that are on a web server. I use PsPad for development and TextPad for things like regular expression finds.

Filed Under Tips | Leave a Comment