O'Reilly logo

Learning Rails 3 by Eric J Gruber, Edd Wilder-James, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix C. An Incredibly Brief Guide to Regular Expressions

Ruby, like many other languages, contains a powerful text-processing shortcut that looks like it was created by cats walking on the keyboard. Regular expressions can be very difficult to read, especially as they grow longer, but they offer tremendous power that’s hard to re-create in Ruby code. As long as you stay within a modest subset of regular expressions, you can get a lot done without confusing anyone—yourself included—who’s trying to make sense out of your program logic.

For a more detailed tutorial, see Mike Fitzgerald’s Introducing Regular Expressions (O’Reilly, 2012). For a much more comprehensive guide to regular expressions, see Jeffrey E. F. Friedl’s classic Mastering Regular Expressions (O’Reilly, 2006) or Tony Stubblebine’s compact but extensive Regular Expression Pocket Reference (O’Reilly, 2007). Jan Goyvaerts’ and Steven Levithan’s Regular Expressions Cookbook (O’Reilly, 2009) is an excellent compendium of ready-to-use expressions and approaches.

What Regular Expressions Do

Regular expressions help your programs find chunks of text that match patterns you specify. Depending on how you call the regular expression, you may get:

A yes/no answer

Something matched or it didn’t

A set of matches

All of the pieces that matched your query, so you can sort through them

A new string

If you specified that this was a search-and-replace operation, you may have a new string with all of the replacements made

Regular expressions ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required