Programming, Regular Expressions, Various Languages

Useful Regular Expressions for Validating a URL and Validating an Email

In this article I’ll cover two useful regular expressions that often come in handy when writing validation logic for a Web page. Most programmers will find they need to either validate a URL or an Email at some point, and the example expressions below are the most commonly used versions of each expression.

I’m not sure who originally created  each of these expressions, so I have no idea who I can give the credit for these to, but they have helped me often. Bottom line for these two examples is that I like to use the example expressions found in an article on NetTuts+/regular-expressions.info and modify them to meet my needs.

Since I’m mentioning the regular-expressions.info site, I definitely suggest browsing over to it if you are looking for further reading about regular expressions including sample code and reference material.

Also, have a look at a free online regular expression tool by Derek Slager. You can use this tool to test your regular expressions if you don’t have a program on your computer to do so. I have spent quite some time trying to find a good online regular expression testing tool, but every one I have found is either too expensive, or full of advertising, so this tool is definitely worth checking out.

Regular Expression for Validating a Web site

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

For starters, you’ll want to make sure that you set your expression to ignore the case, or capital letters in a URL will cause it to fail. Alternately, convert the URL to lower case before applying this expression.

To review the structure of the expression; the ^ symbol matches the start of the line and the $ symbol matches the end of the line. This makes sure that extra text will not be included in valid matches.

The expression logically breaks address matches into four groups. Let’s take http: / / www . google . com / mysearch / android.html as an example; here are how the regular expression matches this URL:

  1. http: / /
  2. www . google
  3. . com
  4. / mysearch/ android.html
  1. The first group search in the regular expression is for the http: / / portion of the Web site address. Since some addresses use https, this is matched by using the s? which means that the s is valid but optional in the match. The ? at the end of this group indicates that it is optional and can be left out but still be a valid URL.
  2. The second group then matches the domain. In the case of our example this is www . google 
  3. The third group then matches the domain suffix. The expression looks for the . and then allows the suffix as a series of at least two letters, but no more than six.
  4. The fourth group matches any remaining parts of the URL containing forward slashes, word characters, periods, and dashes.

Regular Expression for an email address

^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$

This expression checks for valid email address syntax such as myname@yahoo.com 

As with the URL validation expression above, this email expression uses the ^ symbol to match the start of a line and the $ symbol to match the end.

It’s good to note that this expression is not case-sensitive and will match email addresses written in mixed case. You don’t explicitly need to tell your regular expression engine to ignore case.

  1. The first part of this expression ([a-zA-Z0-9._-]+@) captures the name for the email address. This can include alphanumeric characters, periods, underscores, and dashes.
  2. The second part of this expression then captures the domain component of the email address. In this case it is yahoo. The permitted domain characters include alphanumeric characters, periods, and dashes.
  3. The third part of the expression captures the domain suffix. This suffix can only be letter characters and must be at least two characters long, but it cannot be longer than four characters.

Summary

These expressions are definitely very useful and come up time and time again in the tasks of any developer. I hope the descriptions of how they function are of use in deciphering the details of each of these two expressions. Please feel free to add a comment with your favorite expressions for matching an email or Web address.

Advertisement

2 thoughts on “Useful Regular Expressions for Validating a URL and Validating an Email”

  1. Thanks for the marvelous posting! I actually enjoyed reading it,
    you may be a great author.I will be sure to bookmark your blog and definitely will come back in the foreseeable future.
    I want to encourage one to continue your great posts, have a nice day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s