Learn Python Regular Expressions step by step from beginner to advanced levels

Overview

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 — Exercises.md

For solutions to the exercises, see Exercise_solutions.md.

See Version_changes.md to keep track of changes made to the book.


E-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 https://learnbyexample.github.io/py_regular_expressions/


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.

Twitter: https://twitter.com/learn_byexample


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

Acknowledgements

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


License

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

You might also like...
banking system with python, beginner friendly, preadvanced level

banking-system-python banking system with python, beginner friendly, preadvanced level Used topics Functions else/if/elif dicts methods parameters hol

poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions

poetry2nix poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions. It does so by parsing pyproject.t

This tool for beginner and help those people they gather information about Email Header Analysis, Instagram Information, Instagram Username Check, Ip Information, Phone Number Information, Port Scan

This tool for beginner and help those people they gather information about Email Header Analysis, Instagram Information, Instagram Username Check, Ip Information, Phone Number Information, Port Scan. This tool shows your hostname and public IP first, then user give input and according to option this tool work. This tool work diffrent Oprating system.

A set of scripts for a two-step procedure to measure the value of access to destinations across several modes of travel within a geographic area.

A set of scripts for a two-step procedure to measure the value of access to destinations across several modes of travel within a geographic area.

An Advanced Wordlist Library Written In Python For Acm114
An Advanced Wordlist Library Written In Python For Acm114

RBAPG -RBAPG is the abbreviation of "Rule Based Attack Password Generator". -This module is a wordlist generator module. -You can generate randomly

Advanced Developing of Python Apps Final Exercise

Advanced-Developing-of-Python-Apps-Final-Exercise This is an exercise that I did for a python advanced learning course. The exercise is divided into t

Gives you more advanced math in python.

AdvancedPythonMath Gives you more advanced math in python. Functions .simplex(args: {number}) .circ(args: {raidus}) .pytha(args: {leg_a + leg_2}) .slo

Advanced Keylogger in Python
Advanced Keylogger in Python

Advanced Keylogger in Python Important Disclaimer: The author will not be held r

JD-backup is an advanced Python script, that will extract all links from a jDownloader 2 file list and export them to a text file.

JD-backup is an advanced Python script, that will extract all links from a jDownloader 2 file list and export them to a text file.

Comments
  • Possible typo

    Possible typo

    I made it work using 'ice' as the last occurrence with the following expression re.compile(r'hat[\w\s]*(ice)?', re.I) It doesn't seem correct, though.

    opened by gmovchan 4
  • Pull/clone fails on Windows because of invalid characters

    Pull/clone fails on Windows because of invalid characters

    The file code_snippets/Interlude:_Common_tasks.py causes checkout to fail on Windows due to the presence of a colon in the filename.

    λ git clone --depth=1 https://github.com/learnbyexample/py_regular_expressions
    Cloning into 'py_regular_expressions'...
    remote: Enumerating objects: 45, done.
    remote: Counting objects: 100% (45/45), done.
    remote: Compressing objects: 100% (44/44), done.
    remote: Total 45 (delta 3), reused 21 (delta 1), pack-reused 0R
    Receiving objects: 100% (45/45), 892.26 KiB | 3.91 MiB/s, done.
    Resolving deltas: 100% (3/3), done.
    error: invalid path 'code_snippets/Interlude:_Common_tasks.py'
    fatal: unable to checkout working tree
    warning: Clone succeeded, but checkout failed.
    You can inspect what was checked out with 'git status'
    and retry with 'git restore --source=HEAD :/'
    

    Descending into the directory and running git restore --source=HEAD :/ restores the files except "Interlude:_Common_tasks.py". Running git config core.protectNTFS false followed by git checkout also checks out all the files, however "Interlude:_Common_tasks.py" is replaced with a 0-byte file called "Interlude".

    opened by jonathannoah 2
  • Possible misunderstanding for question 10

    Possible misunderstanding for question 10

    Question 10 says: "Match strings that start with den or end with ly", but under "Should NOT match" the string "fly" is listed. I might be wrong, but "fly" ends with "ly" and should match, or am I missing something?

    opened by iLoveTux 1
Owner
Sundeep Agarwal
Addicted to writing books and teaching. Likes fantasy books, comics and anime
Sundeep Agarwal
Beginner Projects A couple of beginner projects here

Beginner Projects A couple of beginner projects here, listed from easiest to hardest :) selector.py: simply a random selector to tell me who to faceti

Kylie 272 Jan 7, 2023
A step-by-step tutorial for how to work with some of the most basic features of Nav2 using a Jupyter Notebook in a warehouse environment to create a basic application.

This project has a step-by-step tutorial for how to work with some of the most basic features of Nav2 using a Jupyter Notebook in a warehouse environment to create a basic application.

Steve Macenski 49 Dec 22, 2022
Step by step development of a vending coffee machine project, including tkinter, sqlite3, simulation, etc.

Step by step development of a vending coffee machine project, including tkinter, sqlite3, simulation, etc.

Nikolaos Avouris 2 Dec 5, 2021
Advanced python code - For students in my advanced python class

advanced_python_code For students in my advanced python class Week Topic Recordi

Ariel Avshalom 3 May 27, 2022
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Advent Of Code 2021 - Python English Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels th

Coral Izquierdo Muñiz 2 Jan 9, 2022
An Advent calendar of small programming puzzles for a variety of skill sets and skill levels.

Advent of Code 2021 The Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be sol

Evan Cope 0 Feb 13, 2022
Fithub is a website application for athletes and fitness enthusiasts of all ages and experience levels.

Fithub is a website application for athletes and fitness enthusiasts of all ages and experience levels. Our website allows users to easily search, filter, and sort our comprehensive database of over 100 exercise types to connect with related equipment and fitness Youtubers. The data is obtained by using eBay and youtube APIs and stored in a mongoDB database

Andrew Wu 1 Dec 13, 2021
A library for pattern matching on symbolic expressions in Python.

MatchPy is a library for pattern matching on symbolic expressions in Python. Work in progress Installation MatchPy is available via PyPI, and

High-Performance and Automatic Computing 151 Dec 24, 2022
Waydroid is a container-based approach to boot a full Android system on a regular GNU/Linux system like Ubuntu.

Waydroid is a container-based approach to boot a full Android system on a regular GNU/Linux system like Ubuntu.

WayDroid 4.7k Jan 8, 2023
Python Project For Beginner

Basic-Vitrual-AI-Assistant Python Project For Beginner Hey There, I had manipulated Selenium WebDriver to make this assistant. I hope, It will be help

Maruf Billah 13 Dec 12, 2022