Preface
Scripting and automation tasks often need to extract particular portions of text from input data or modify them from one format to another. This book will help you learn Regular Expressions, a mini-programming language for all sorts of text processing needs.
The book heavily leans on examples to present features of regular expressions one by one. It is recommended that you manually type each example and experiment with them. Understanding both the nature of sample input string and the output produced is essential. As an analogy, consider learning to drive a bike or a car — no matter how much you read about them or listen to explanations, you need to practice a lot and infer your own conclusions. Should you feel that copy-paste is ideal for you, code snippets are available chapter wise on GitHub.
Prerequisites
You should have prior experience working with Ruby, should know concepts like blocks, string formats, string methods, Enumerable, etc.
If you have prior experience with a programming language, but new to Ruby, check out my GitHub repository on Ruby Scripting before starting this book. That repository also includes a chapter on Regular Expressions which has been edited and expanded to create this book.
Conventions
- The examples presented here have been tested with Ruby version 3.0.0 and includes features not available in earlier versions.
- Code snippets shown are copy pasted from
irb --simple-prompt
shell and modified for presentation purposes. Some commands are preceded by comments to provide context and explanations. Blank lines have been added to improve readability.nil
return value is not shown forputs
statements. Error messages are shortened. And so on. - Unless otherwise noted, all examples and explanations are meant for ASCII characters.
- External links are provided for further reading throughout the book. Not necessary to immediately visit them. They have been chosen with care and would help, especially during re-reads.
- The Ruby_Regexp repo has all the code snippets and files used in examples and exercises and other details related to the book. If you are not familiar with
git
command, click the Code button on the webpage to get the files.
Acknowledgements
- ruby-lang documentation — manuals and tutorials
- /r/ruby/ and /r/regex/ — helpful forum for beginners and experienced programmers alike
- stackoverflow — for getting answers to pertinent questions on Ruby and regular expressions
- tex.stackexchange — for help on pandoc and
tex
related questions - Cover image:
- pngquant and svgcleaner for optimizing images
- Warning and Info icons by Amada44 under public domain
- softwareengineering.stackexchange and skolakoda for programming quotes
- gmovchan for spotting a typo
- mdBook — for web version of the book that you are currently reading
- mdBook-pagetoc — for adding table of contents for each chapter
- minify-html — for minifying html files
Special thanks to Allen Downey, an attempt at translating his book Think Python to Think Ruby gave me the confidence to publish my own book.
Feedback and Errata
I would highly appreciate if you'd let me know how you felt about this book, it would help to improve this book as well as my future attempts. Also, please do let me know if you spot any error or typo.
Issue Manager: https://github.com/learnbyexample/Ruby_Regexp/issues
E-mail: learnbyexample.net@gmail.com
Twitter: https://twitter.com/learn_byexample
Author info
Sundeep Agarwal is a lazy being who prefers to work just enough to support his modest lifestyle. He accumulated vast wealth working as a Design Engineer at Analog Devices and retired from the corporate world at the ripe age of twenty-eight. Unfortunately, he squandered his savings within a few years and had to scramble trying to earn a living. Against all odds, selling programming ebooks saved his lazy self from having to look for a job again. He can now afford all the fantasy ebooks he wants to read and spends unhealthy amount of time browsing the internet.
When the creative muse strikes, he can be found working on yet another programming ebook (which invariably ends up having at least one example with regular expressions). Researching materials for his ebooks and everyday social media usage drowned his bookmarks, so he maintains curated resource lists for sanity sake. He is thankful for free learning resources and open source tools. His own contributions can be found at https://github.com/learnbyexample.
List of books: https://learnbyexample.github.io/books/
License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Code snippets are available under MIT License
Resources mentioned in Acknowledgements section above are available under original licenses.
Book version
2.6
See Version_changes.md to track changes across book versions.