Lesson 35 of 60
โฎPrev Nextโฏ

PY-101 ยท Strings in depth

Regular expressions basics

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

35of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A regular expression is a pattern that describes text, like \d+ for "one or more digits". Python's re module uses it to search: re.findall(pattern, text) returns every match, re.search finds the first, and re.sub replaces matches. Write patterns as raw strings, r"...", so backslashes stay as typed.

    • Read simple patterns (\d, \w, +)
    • Find all matches with re.findall
    • Replace with re.sub

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Regular expressions basics, running
    1. import re
    2. msg = "Hi, orders ORD-1042 and ORD-77 are late. Call 98765 43210."
    3. print(re.findall(r"ORD-\d+", msg))
    4. first = re.search(r"\d+", msg)
    5. print("First number:", first.group())
    6. print(re.sub(r"\d", "#", "PIN 4821"))
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA support inbox has 2,000 messages and you need every order number, which looks like ORD- followed by digits. Searching by hand would take a day; a regex takes a line.
    2. The Python ideaimport reA regular expression is a pattern that describes text, like \d+ for "one or more digits".
    3. In real lifeBanks mask account numbers in emails and logs with re.sub so only the last digits are ever visible.

    Start with a question

    A support inbox has 2,000 messages and you need every order number, which looks like ORD- followed by digits. Searching by hand would take a day; a regex takes a line.

    Validating input, extracting IDs from logs and cleaning data all rely on regular expressions.

    Explain it like I'm 10

    A regex is a "wanted poster" for text: "looks like three digits, a dash, then four digits". Python checks every word against the poster.

    The technical explanation

    \d is a digit, \w a letter, digit or underscore, \s a space. + means one or more, * zero or more.

    re.findall(r"\d+", text) returns a list of every match as strings.

    re.sub(r"\d", "#", text) replaces every digit with #.

    Syntax

    import re
    re.findall(r"pattern", text)
    re.search(r"pattern", text)
    re.sub(r"pattern", replacement, text)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Banks mask account numbers in emails and logs with re.sub so only the last digits are ever visible.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does \d match?
    2. 2. What does + mean in a pattern?
    3. 3. re.findall(r"\d+", "a1b22") returns?
    4. 4. Why use r"..." for patterns?
    5. 5. Which replaces matches?

    Questions people ask

    When should I use regex instead of string methods?

    Use string methods for fixed text (startswith, split on a comma). Use regex when the text follows a pattern, like "letters, an @, then a domain".

    How do I make a regex ignore case in Python?

    Pass flags=re.IGNORECASE, for example re.findall(r"python", text, flags=re.IGNORECASE).

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner When should I use regex instead of string methods?

    Use string methods for fixed text (startswith, split on a comma). Use regex when the text follows a pattern, like "letters, an @, then a domain".

    beginner How do I make a regex ignore case in Python?

    Pass flags=re.IGNORECASE, for example re.findall(r"python", text, flags=re.IGNORECASE).

    intermediate What does \d match?

    A digit. \d is a digit 0โ€“9.

    intermediate What does + mean in a pattern?

    One or more. + repeats one or more times.

    Quick revision

    Remember

    • Read simple patterns (\d, \w, +)
    • Find all matches with re.findall
    • Replace with re.sub
    • Always use raw strings for patterns: r"\d+". Without the r, Python may treat backslashes as its own escape codes.

    Syntax

    import re
    re.findall(r"pattern", text)
    re.search(r"pattern", text)
    re.sub(r"pattern", replacement, text)

    Official documentation