Preparations

What Do I Need To Know?

The sole prerequisite for this book is a familiarity with Ruby, JavaScript, Python, or a similar language. Going forward, we'll call the three main languages the Big Three for brevity. It is for students who need to learn about regex in general, so it strives to be language-agnostic and does not rely heavily on knowledge obtained elsewhere. However, some of the examples and exercise solutions use the Big Three as the languages of choice. If you have experience with Perl or PHP, you should be able to get by, even if you don't know one of the Big Three well.

Don't let this stop you if you are learning regex for a different language; regex have some variants, but the basics are compatible across languages. Learning regex for the Big Three will help you in any application that supports regex. Read the subsection on Variants in the Conclusion if you must learn regex for another language.

That said, you should find the regex documentation for your software and keep it close by for reference. Once you've learned the basics, use the documentation to gain proficiency with regex. In particular, rubyists should find the documentation for the Regexp class valuable; JavaScripters need the RegExp documentation (note the capitalization); and Pythonistas need the re module documentation. The Big Three provide support for commonplace regex operations with their respective string classes.

Useful Learning Resources

The web offers up regex test beds for each of the Big Three languages, and there are certainly similar sites for other languages:

  • For Ruby, Rubular is a great way to explore how regex interact with the strings you want to process.
  • For JavaScript, you can use Scriptular. Regexr is an alternative to Scriptular. It has some nice features, such as a graphic depiction of how your regex is interpreted.
  • For Python, give pythex a try.

All three sites let you enter regex and sample strings to test the regex against, alter the regex flags, and provide a handy quick reference. These services are so useful that we rely heavily on them to demonstrate how regex work with various strings.

Due to differences in the way Rubular, Scriptular, and pythex work, we use Rubular for our discussions. Even if you are learning regex for JavaScript or Python, we recommend that you use Rubular while working through this book. If you use Scriptular or pythex instead, the results may be confusing. In particular, Scriptular handles newlines differently from Rubular and this alters the results expected with some problems. Scriptular also requires the /g flag to show multiple matches on a single line.

When we discuss the regex test beds, we'll often say that a regex highlights or lights up a particular string or strings. This is shorthand for saying that the test bed should have highlighted the indicated items when it applied a regex to some test data.

Summary

With this basic information out of the way, you are now ready to make your first dive into the world of regex. Enjoy!