Totally Communications Ltd

Websites. Software. Consulting

Totally Communications specialise in professional web design, software development and online marketing, working with SME's, charities and large corporates. more >>

Call to Discuss Your Project

Regular Expressions for beginners

By Simon Vandyk

Added Monday 29/09/2008

Well, It's my turn to post again but unfortunately not with what many would call an "Interesting" subject, but for what they lack on that front they make up in usefulness, versatility and just pure genius!

In fact I can honestly say that I've used them every day at work since learning how they worked; you guessed what they are yet? REGULAR EXPRESSIONS!

Excuse my sad excitement when I mention the name, but I have seriously grown to appreciate the enormous power these things have and the amount of time and frustration they can alleviate.

Having looked at regular expressions for the past 2 years, with only a rudimentary knowledge of what they were or how they worked, I finally decided to bite the bullet and learn how to use them, and can only say that I haven't regretted it since. The only disappointment is that I had not got around to learning them sooner; because I imagine the amount of time they could have saved me would probably have worked out to a millennia.

I suppose I should finally get around to explaining what regular expressions are; putting it into the simplest way possible a regular expression is an "advanced search".

When you search for a file on your computer or a word in a word document, what do you do? You go to the search tool, type in what you want to find and click search; fairly simple I'd say, but what if I'm searching in my word document for a particular pattern of words and letters i.e. an email address, I in fact want to find all the email addresses in my word document; using the search tool in the usual way would mean I have to type in each email address in into the search tool and click search, then enter the next one into the search box, click search, and so on.

Its at times like this that can get you very frustrated and think there must be an easier and less time consuming way to do this. Well there is, a regular expression allows you to search for the pattern that makes up an email address using special character sequences, so instead of typing into the search box:

'someone@anemailaddress.com'

I would type in:
^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$

This may look like gobbledygook but it's a regular expression, and it will find nearly every email address in my document. Click here here for an detailed explanation of how it works

In most advanced text editors such as Eclipse or EditPlus you can use this type of search just as long as the regular expression tick box checked (if you have a regular expression tick box! Otherwise find another editor to search in which has it, or use the the command line with commands like grep and egrep).

Regular expressions even have the ability to remember what results it picked up with the patterns you entered so you can use them later.

The regular Expression Tutorial is a good place to go if you want to learn how to use regular expressions properly. As you can see my explaining it here would be too much for me and you. I can't think of how much time this would have saved me in school, university and work.

Definitely worth learning and don't put it off like I did!


Bookmark and Share

Comments

There are no comments at this time.

Toby Mason - Google Certified Professional Certification IMA Award Winner SEO Expert Rating Professional Valid XHTML 1.0 Transitional E-Consultancy Internet Advertising Bureau

Totally Communications 2010
Software Developers London |

SEO Company London | PPC Company London | Search Marketing Company London