Python 3D Academyবাংলা

Repeats and anchors: checking a whole format

🌍 In real life. A sign-up form asks for a Bangladeshi mobile number. A real one has 11 digits, starts with 01, and its third digit is 3 to 9, like 01712345678. Checking that character by character takes many ifs. One pattern can describe the whole format.

The tools, step by step:

  1. repeats go right after the thing they repeat: + one or more, * zero or more, ? optional (zero or one), {8} exactly 8, {2,4} from 2 to 4
  2. anchors match a place, not a character: ^ the start of the text, $ the end
  3. re.fullmatch(pattern, text) only matches if the pattern covers the whole text, which is exactly what checking a format needs

The idea in one sentence: repeat marks like {11} and ? say how many times something appears, anchors like ^ and $ pin a pattern to the start and end, and re.fullmatch checks that the whole text fits.

Its words:

  • {n}, {m,n}: exactly n times, or between m and n times
  • ? and *: zero or one time; zero or more times
  • character class [3-9]: any one character from the set
  • re.fullmatch: the pattern must cover the text from first character to last
import re

phone = r"01[3-9]\d{8}"
for number in ["01712345678", "01212345678", "0171234567", "+8801712345678"]:
    print(number, bool(re.fullmatch(phone, number)))
01 is matched as itself, [3-9] is one digit from 3 to 9, and \d{8} is exactly eight more digits: 11 in all.

Watch it check a number. fullmatch only says yes when the lens can stretch from the very first character to the very last.

search is happy with a piece; fullmatch needs it all. re.search finds a phone number somewhere, so "call 01712345678 now" passes, and so does "0171234567899999" (it contains 11 good digits). That's right for finding numbers inside a message, but wrong for checking a form. For checking, use fullmatch (or search with ^ and $).

Repeats are greedy: they take as much as they can while the rest of the pattern still fits. \d{2,3} on "4444" takes 444, not 44.

import re

pin = r"[1-9]\d{5}"  # an Indian PIN code: 6 digits, not starting with 0
for text in ["110001", "011000", "pin 110001 ok"]:
    print(repr(text), bool(re.fullmatch(pin, text)), bool(re.search(pin, text)))

print(re.findall(r"colou?r", "color or colour"))
print(re.findall(r"\d{2,3}", "1 22 333 4444"))

Why fullmatch for checking a form? search is happy with any piece that fits, so a phone number with extra digits on the end would pass. fullmatch says yes only when nothing is left over at either end.

Mistakes and edge cases.

  • search for validation. "0171234567899999" passes re.search(r"01[3-9]\d{8}", ...) because it contains 11 good digits.
  • A space in {8, 9}. Inside braces, write {8,9} with no space.
  • [3-9] for one digit only. A character class matches one character; repeat it with {n} for more.
  • Forgetting that . matches anything. In a pattern, . is any character; write \. for a real dot.

Quiz

  1. What does this print?

    import re
    
    print(bool(re.fullmatch(r"\d{4}", "1207")))
  2. What does this print?

    import re
    
    print(bool(re.fullmatch(r"\d{4}", "12077")))
  3. What does this print?

    import re
    
    print(re.findall(r"colou?r", "color colour"))
  4. Which pattern matches one or more letters a, like a, aa or aaa?

    • a+
    • a?
    • +a
  5. What does this print?

    import re
    
    print(bool(re.search(r"^\d+
    quot;, "42 apples")))
  6. A sign-up form should reject 0171234567899 (too long), but this accepts it. Which line has the mistake?

    import re
    number = "0171234567899"
    ok = re.search(r"01[3-9]\d{8}", number)
    print(bool(ok))

Coding tasks

  1. Write valid_phone(number: str) -> bool: True only for a Bangladeshi mobile number, 11 digits that start with 01 and have a third digit from 3 to 9.

  2. Write valid_pin(code: str) -> bool: True for an Indian PIN code, exactly 6 digits where the first isn't 0.

  3. Write strong_password(pw: str) -> bool: True when the password has at least 8 characters, at least one letter and at least one digit.