Regular Expression Patterns

The re module provides a comprehensive collection of pre-defined regular expressions for common data validation tasks. These patterns are designed to handle various validation scenarios across different fields, making input validation simpler and more reliable.

Categories

The module includes patterns for the following categories:

  1. Username Validation

  2. Password Validation

  3. Email Validation

  4. Phone Number Validation

  5. Zip Code Validation

  6. Address Validation

  7. Credit Card Validation

  8. IP Address Validation

  9. Date Validation

  10. URL Validation

Username Patterns

true.re.USERNAME_ONLY_LETTERS_MIN_3: str

Matches usernames containing only letters with a minimum length of 3 characters.

Example:

  • “John” matches

  • “Jo” doesn’t match (too short)

  • “John123” doesn’t match (contains numbers)

true.re.USERNAME_LETTERS_AND_NUMBERS_MIN_3: str

Matches usernames containing letters and numbers with a minimum length of 3 characters.

Example:

  • “John123” matches

  • “Jo” doesn’t match (too short)

  • John@123” doesn’t match (contains special characters)

Password Patterns

true.re.PASSWORD_MIN_8_WITH_NUMBER: str

Matches passwords with a minimum length of 8 characters that include at least one number.

Example:

  • “password123” matches

  • “password” doesn’t match (no number)

  • “pass123” doesn’t match (too short)

true.re.PASSWORD_MIN_8_WITH_UPPERCASE: str

Matches passwords with a minimum length of 8 characters that include at least one uppercase letter.

Example:

  • “Password123” matches

  • “password123” doesn’t match (no uppercase)

  • “Pass123” doesn’t match (too short)

Credit Card Patterns

true.re.CREDIT_CARD_BASIC: str

Matches basic credit card numbers with optional spaces or dashes.

Example:

  • “1234 5678 9012 3456” matches

  • “1234-5678-9012-3456” matches

  • “1234.5678.9012.3456” doesn’t match (invalid separator)

URL Patterns

true.re.URL_WITH_PORT: str

Matches URLs that include a port number.

Example:

true.re.URL_WITH_SUBDOMAIN: str

Matches URLs that include subdomains.

Example:

Usage

To use these patterns in your code:

from true.re import USERNAME_ONLY_LETTERS_MIN_3
import re

# Validate a username
username = "John"
if re.match(USERNAME_ONLY_LETTERS_MIN_3, username):
    print("Valid username!")
else:
    print("Invalid username!")