This is an area that I had/have very little experience with, but is one of the most powerful tools a web developer can use. If you don’t know what it is, I suggest you learn.
So, what is a regular expression? “A regular expression (regex or regexp for short) is a special text string for describing a search pattern.” (Regular-Expressions.info)
You might be thinking “Okay, but what does that mean?” It basically means you can define a set of symbols and letters to find a string or character within a string. A string could be defined as a certain set of letters, words, numbers, symbols or a combination of them all. For example, the regular expression “[0-9]” would find the first digit in this phone number string “(800)123-4567″. It would return saying it found the letter 8.
They are a big use in validating an input on a form. For example, lets say on your Contact page you have an e-mail form where they are asked to put in their name, email and a message to you. Well, you don’t want people to give you a bogus email do you? Using a regular expression like this: “\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b” and that will make sure they use the right email. Of course that is extremely confusing if you don’t know what they do, so that’s why I’m writing this blog so you have some resources so that you can learn or just get regular expressions.
Probably the best site to get regular expressions from is RegExLib.com, where thousands of users submit their own RegEx’s and are all available to you. They have a search so you can search “email” and it will give you tons of different examples, or “date” (I made one that I will get to in a bit) and so on. It also has a resources section, which includes some free downloadable programs that will help you validate or create regular expressions. I got one of them, The Regex Coach. Unfortunately, it tries to be helpful and auto update as you type, but it can severly lag the program. Although this feature can be useful, I’d rather have a hotkey to press to check the Regex, so I might be searching for another program soon.
Another useful link is RegExAdvice.com, where they have a forum where you can ask for help with your regex. There are also tons of blogs specifically about regex, so if you need help with someone from actual live people, this is the place to go. I haven’t actually done that much here, mainly because I hate waiting for other people to continue working, I like finding the answer myself.
Tutorials. Well, that’s probably what I should have infoed you about first. I would have, too, if I had gone in that order, but I only did my tutorial after the rest of my studying recently. I’ve done some here and there but they were really confusing and could only vaguely grasp the concepts. Well, I was searching for a good one so that I could recommend it to other people, and I found one: Regular-Expressions.info. Here you will see some of the same examples I gave at the top, and also a “quick” tutorial. I did that quick tutorial tonight, and though it’s a lot of data to grasp at once (I don’t recommend trying to go through the whole thing in one go), it’s very good. If you have seen regular expressions before and only have a half understanding like I did, it goes over a lot of the symbols that are used and makes things make so much more sense. Apart from their quick tutorial which is divvied up into sections, they also have thorough tutorials on each section (not a paragraph or two). I’m probably going to go through those one at a time to make sure I really understand everything.
Of course that’s the free way of learning, and it’s how I’ve learned pretty much all of my web knowledge. You can, however, also buy books. I’ve had some good recommendation for O’Reilly books (like on CSS and HTML/XHTML), so I checked if there was an O’Reilly Regular Expressions book. They actually have two, a “Pocket Reference” and one called “Mastering Regular Expressions“. I plan on getting the latter first, and once I do “master” them get the pocket reference.
I actually do often learn a lot just by pocket references, or cheatsheets, things that just give you the raw data for you to program. They’re quick and get to the point, and I learned about about regular expressions from RegExLib.com’s Cheatsheet, which goes over a quick definition of pretty much all the symbols, though they aren’t necessarily complete.
Oh, I thought I should note that you need to be careful if you are using a script (such as javascript) that uses “\” for other functions (like “\n” means new line). For instance, “[0-9]” and “[\d]” both specify a single digit and are completely identical, but in javascript you can’t say “[\d]“, you have to say “[\\d]“.
This is to make sure a field contains a format in the php datetime (includes both the date and time) format for a date only, which is “yyyy-mm-dd”: “^[0-9]{4}-[0-9]{2}-[0-9]{2}$”. If you don’t have that specific format, my javascript validator will alert you.
The other one I made, which I reluctantly realized had already been created by someone on RegExLib, is one that validates image extensions. It is true that mine is slightly better in the fact that it validates the whole image file name, and not just the extension, but here you go:
“^[0-9A-Za-z_ \-]+(.[jJ][pP][gG]|.[jJ][pP][eE][gG]|.[gG][iI][fF]|.[pP][nN][gG])$”. I might change it a bit later so that you can enter a folder name, like “/images/logo.jpg” and it will work, rihgt now you have to enter just the name “logo.jpg”. That verifies the formats jpg, jpeg, gif and png.
One last thing, if you need an online validator, RegExLib.com has one and the one I use implements the javascript regex function (as I told you there can be slight differences depending what regex engine you’re using), so that I get as close to right for my javascript validation as possible before I implement. Here’s the link: Regular Expression Tester.
I will probably be doing more updates once I get the O’Reilly book on this subject, or if I come up with more cool regexs, but this is it for now.
-Kerry
P.S. In case anyone is wondering, I am not trying to “promote” these sites or companies, I don’t get any money for it and I don’t have some agreement where I link to them and they link to me (I wish), it’s for the sole purpose that you’ll have the references you need.