Regular Expressions are one of the most powerful tools in a developers arsenal. Yet, I’m amazed at the number of developers who jump through hoops to avoid using them because they aren’t comfortable working with them. I have a simple rule about regular expressions. Every application should use Regular Expressions. Sure, there are exceptions to this rule but I find them to be very rare. I don’t mean to say you need to look for uses of Regular Expressions. In most web applications there is a case where they are just the best tool for the job.
Regular Expressions Are Everywhere
If you aren't familiar with regular expressions, they are everywhere. When you type an email address into a field and the software converts it to a link that's a regex. When you type in a @ before a twitter username and it knows it's a reply to the user that's a regular expression.Simply put, a regular expression allows you to query patterns in text. If you want, you can act on those patterns. Think of it as a really powerful text search.
Regular Expressions For Validation
Some of the uses of Regular Expressions are sexy like ones that convert your sites content into pirate talk. Others are more practical and useful. Have you ever wanted to verify that a user entered a valid type of email address into a field? One that's in the correct format. To do this you would use a regular expression. Many tools provide validation functions or classes built in that do this to save time. But, the underlying validation is a regular expression.Tutorials and Guides
I'm not going to write out my own tutorials. Why try to reinvent the wheel. Here are some good ones to take for a spin.- Regular-Expressions.info - A one stop shop for regular expression knowledge.
- Mastering Regular Expressions - Yes, a book. Some people prefer books and much of the best information out there is only available in book format.
- Using Regular Expressions with PHP - PHP is the most popular of the web programming languages. This is a quick guide to using regex in PHP.
- Python Regular Expression HowTo - A guide to using regex in Python.
- Regular Expression Cheat Sheet - Who doesn't love a good cheat sheet!
If you’re at all nervous take some time and work through some of these regular expression tutorials in your favorite language. You’ll find a load of uses for them and feel more comfortable with one of the most powerful tools in a programmers toolkit.