Appendix C. An Incredibly Brief Guide to Regular Expressions
Music is the soul of language.
Max Heindel
If music is the soul of language, regular expressions are the soul of text processing. 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). Regular Expressions Cookbook (OâReilly, 2012) by Jan Goyvaerts and Steven Levithan 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 ...
Get Learning Rails 5 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.