11.2. Regexp Match

11.2.1. About

  • re.match()

  • Checks exact match

  • Checking if user input is correct (email, url, NIP, VAT ID, PESEL)

11.2.2. Example

Listing 406. Usage of re.match()
import re


def is_valid_email(email: str) -> bool:
    PATTERN = r'^[a-zA-Z0-9][\w.+-]*@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,20}$'

    if re.match(PATTERN, email):
        return True
    else:
        return False


is_valid_email('[email protected]')     # True
is_valid_email('[email protected]')       # False

11.2.3. Good Engineering Practices

  • Doctests

Listing 407. Doctests
import re


def is_valid_email(email: str) -> bool:
    """Function check email address against Regular Expression
    >>> is_valid_email('[email protected]')
    True
    >>> is_valid_email('[email protected]')
    True
    >>> is_valid_email('[email protected]')
    False
    >>> is_valid_email('[email protected]')
    True
    >>> is_valid_email('[email protected]')
    True
    >>> is_valid_email('[email protected]')
    False
    >>> is_valid_email('@nasa.gov')
    False
    >>> is_valid_email('[email protected]')
    False
    """
    PATTERN = r'^[a-zA-Z0-9][\w.+-]*@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,20}$'

    if re.match(PATTERN, email):
        return True
    else:
        return False

11.2.4. Assignments

11.2.4.1. Phone Number Validation

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Filename: solution/match_phones.py

English
  1. Use regular expressions to validate phone numbers

  2. Check all given numbers (see input section)

  3. Valid phone number formats: +## ### ### ### or +## ## ### ####

Polish
  1. Użyj wyrażeń regularnych do walidacji numeru telefonu

  2. Sprawdź wszystkie podane numery (patrz sekcja input)

  3. Poprawne formaty numeru: +## ### ### ### lub +## ## ### ####

Input
+48 (12) 355 5678
+48 123 555 678
123 555 678
+48 12 355 5678
+48 123-555-678
+48 123 555 6789
+1 (123) 555-6789
+1 (123).555.6789
+1 800-python
+48123555678
+48 123 555 678 wew. 1337
+48 123555678,1
+48 123555678,1,2,3

11.2.4.2. Git Flow Branch Names

  • Complexity level: medium

  • Lines of code to write: 15 lines

  • Estimated time of completion: 20 min

  • Filename: solution/match_gitflow.py

English
  1. Use regular expressions to validate Git branch names

  2. Check all given branch names (see input section)

  3. Branch names should comply with Git Flow convention:

  4. Where:

    • release/major.minor - major and minor are unsigned integers

    • feature/, bugfix/, hotfix/ - branch prefixes

    • prefix/ISSUEKEY-NUMBER-summary

    • ISSUEKEY - uppercase, only ASCII letters, minimum 2 characters, not longer than 10

    • NUMBER - positive integer, maximal 5 digits

    • summary - lowercase, ASCII letters and numbers, dashes instead whitespaces, not longer than 30

    • pull-request/NUMBER - positive integer, maximal 5 digits

  5. Example of valid branches:

    • master

    • develop

    • release/1.0

    • feature/ID-1337-some-new-feature

    • bugfix/ID-1337-fixing-old-bug

    • hotfix/ID-1337-bug-on-production

    • pull-request/42

Polish
  1. Użyj wyrażeń regularnych do walidacji nazwy gałęzi w Git

  2. Sprawdź wszystkie dane nazwy gałęzi (patrz sekcja input)

  3. Nazwy gałęzi powinny stosować się do konwencji Git Flow:

    • release/major.minor - major i minor nieujemne liczby całkowite

    • feature/, bugfix/, hotfix/ - prefiks nazwy gałęzi

    • prefix/ISSUEKEY-NUMBER-summary

    • ISSUEKEY - duże litery, tylko litery ASCII, minimum 2 znaki, nie więcej niż 10

    • NUMBER - dodatnia liczba całkowita, maksymalnie 5 cyfr

    • summary - małe litery, litery ASCII i liczby, myślniki zamiast białych spacji, nie dłuższa niż 30

    • pull-request/NUMBER - dodatnia liczba całkowita, maksymalnie 5 cyfr

  4. Przykład poprawnych gałęzi:

    • master

    • develop

    • release/1.0

    • feature/ID-1337-some-new-feature

    • bugfix/ID-1337-fixing-old-bug

    • hotfix/ID-1337-bug-on-production

    • pull-request/42