Awesome Open Source
Awesome Open Source

Python re(gex)?

Learn Python Regular Expressions step by step from beginner to advanced levels with hundreds of examples and exercises

The book also includes exercises to test your understanding, which is presented together as a single file in this repo —

For solutions to the exercises, see

See to keep track of changes made to the book.


For a preview of the book, see sample chapters

The book can also be viewed as a single markdown file in this repo. See my blogpost on generating pdf from markdown using pandoc if you are interested in the ebook creation process.

For web version of the book, visit

Feedback and Contributing

Open an issue if you spot any typo/errors.

⚠️ ⚠️ Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.

I'd also highly appreciate your feedback about the book.


Table of Contents

  1. Preface
  2. Why is it needed?
  3. re introduction
  4. Anchors
  5. Alternation and Grouping
  6. Escaping metacharacters
  7. Dot metacharacter and Quantifiers
  8. Interlude: Tools for debugging and visualization
  9. Working with matched portions
  10. Character class
  11. Groupings and backreferences
  12. Interlude: Common tasks
  13. Lookarounds
  14. Flags
  15. Unicode
  16. regex module
  17. Gotchas
  18. Further Reading


Special thanks to Al Sweigart, for introducing me to Python with his awesome automatetheboringstuff book and video course.


The book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

The code snippets are licensed under MIT, see LICENSE file

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (50,901
regex (160
ebook (104
learning-by-doing (55
exercises (37
regular-expressions (32

Find Open Source By Browsing 7,000 Topics Across 59 Categories