Rex Eats Regular Expressions for Breakfast


Rex eats regular expressions for breakfast. And so can you! This regex tutorial, one of the most detailed on the web, takes you all the way to mastery.

But first, if you have time, an introduction about this site and what makes it special.

I love regular expressions. They are a small computer language of their own.
When I was a young dinosaur, I didn't take the time to properly learn the syntax, largely because I really didn't feel like learning another language. Who needs regex, I thought, when your programming language has functions that let you dig into strings from the left, the middle and the right?

What's more, the raw syntax you usually see in code that contains regexes used to intimidate me. Who wants to deal with a language that looks like this?

(?s)/\*(?:(?!\*/)[*$ _/+\\-])*(.*?)[*$ _/+\\-]*?\*/

It is well worth investing a bit of time in Regular Expressions. You won't look back!
As it turns out, you really don't have to write your regular expressions like this. In many regex flavors, such as the PCRE flavor found in PHP, you can aerate your regex just like code, inserting comments on each line. If you walk with me through this site, you will be able to understand the expression above. Just as a preview, here is how the very same regex might look once "aerated" and commented, on multiple lines:

(?xs)                   # this version of the same regex has comments!

/\*                     # blah blah

(?:(?!\*/)[* _/+\\-])*  # blah again

(.*?)                   # more blah

[*$ _/+\\-]*?           # yadda yadda blah

\*/                     # that was clear as day, wasn't it?

No doubt about it, even with comments and aeration, there is something raw and experimental about writing a regex pattern. And how well your pattern performs doesn't just depend on how carefully you applied the syntax. For complex matches in long texts, it also depends on the optimizations built into the particular regex engine of the environment you are using—whether that's Java, PHP, or perhaps a fancy renaming tool. With regex, you are stepping down to a fairly low level, within earshot of the machine room. I like that. And I like it all the more since I have learned about a few tools and safeguards to save me from falling into the boiler.

A Different Presentation of Regex (Hopefully)

To really learn, you need to see the same information in different ways.
There are excellent web pages about regex. Not many, but there are some, and I reference my favorite ones throughout the site. Then there are many pages that repeat the same old syntax reference. The problem is that for unfamiliar technical information to anchor itself in your mind—or at least in mine—, you have to see it presented from various angles. When I started learning regex, because the information I found was so homogeneous as I was hopping from page to page and book to book, the "information tree" wasn't yielding all its fruits, and several questions that cut diagonally through the field of regex were staying unresolved.

This site tries to present regular expressions a bit differently, in the hope that these different angles help many people become more grounded in their knowledge of regex. If you are looking for a drawn-out primer, this is not the place, as I don't see the need to pollute our beautiful world wide web with another explanation of how to match "foo" in "foo bar". But if you take your time to read the carefully built tables on the quick-launch page, you will experience what may be the most accelerated regex introduction around.

And just in case the "foo" in "foo bar" really is all that you want to match, here's the regex for it. Feel free to copy and-paste it into your application: foo

What Will you Find on this Site

Oh, yes, and forget about practice, that's completely overrated. Just kidding.
Get ready, because as far as I know, this site is one of the two most comprehensive regex sources on the net (along with Jan Goyvaerts excellent regex tutorial site). It aims to fill gaps in how regex information is presented elsewhere, including the major regex books. Here are some of the things you will find here.

✽ A step-by-step explanation of simple and advanced regular expressions crafted for various contexts (such as text matching, file renaming, search-and-replace).

✽ A presentation of the many contexts where you may run into regular expressions (from Apache to your html editor and file manager), complete with examples.

✽ A reference about (?—to reduce confusion by bringing all the pieces of syntax that start with these two letters into a single place.

✽ A discussion of Conditional Regexes, a topic about which there is little information.

✽ A discussion of Recursive Regexes, a topic about which there almost no information.

✽ A section dedicated to PHP Regex, including features you won't find in the PHP manual!

✽ Plenty of tips & tricks.

✽ Sections about regex tools and regex books.

✽ And much more!

I wish you lots of fun on your journey with regular expressions.

Smiles,

Rex

next
 Quick-Start: Regex reference table




Be the First to Leave a Comment






All comments are moderated.
Link spammers, this won't work for you.

To prevent automatic spam, we require that you type the two words below before you submit your comment.