Python RegEx Tester

Search...

⌘K

Python RegEx Tester

Search...

⌘K


Python RegEx Tester

Python RegEx Tester

Test and debug Python regular expressions online with the Qodex Python Regex Tester. Instantly highlight matches and refine patterns used for email validation, password checks, phone number validation, and more. Use data from the Email Generator or UUID Generator, and compare behavior with the Java or JavaScript Regex Testers.

john@qodex.ai
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "john@qodex.ai" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is Python Regex?


Python uses the built-in re module to support regular expressions. Regex allows you to match, extract, and transform text using patterns. It’s widely used in:


  • Data validation (e.g., email, password, phone numbers)

  • Text processing and cleanup

  • Web scraping and log analysis

  • Extracting patterns from strings


Core Components of Python RegEx

  • Real-Time Matching – Immediate pattern matching and highlight as you type.

  • Supports Python re Syntax – Works exactly like Python’s regex engine.

  • Capturing Groups Displayed – Shows capture groups and matches.

  • Beginner-Friendly – Just paste your regex and test string—no coding required.

  • Integrates with Test Tools – Try with Address Generator, Password Generator, or MAC Address Generator.


Metacharacters

  • . - Matches any character except newline (\n).

    Example: a.b matches acb, a9b, etc., but not ab.

  • ^ - Matches the start of a string.

    Example: ^Hello matches “Hello world” but not “Say Hello”.

  • $ - Matches the end of a string or just before the newline at the end.

    Example: world$ matches “Hello world” but not “world peace”.

  • | - Acts as a logical OR operator.

    Example: cat|dog matches either “cat” or “dog”.


Character Classes

  • [abc] - Matches any one of a, b, or c.

    Example: gr[ae]y matches both “gray” and “grey”.

  • [^abc] - Negates the set. Matches any character except a, b, or c.

    Example: [^0-9] matches any non-digit.

  • [a-zA-Z] - Matches any alphabet character.

    Example: [A-Z] matches uppercase letters only.


Predefined Character Classes

  • \d : Matches any digit character; equivalent to [0-9].

  • \D : Matches any non-digit character.

  • \s : Matches any whitespace character: space, tab, newline, etc.

  • \S : Matches any non-whitespace character.

  • \w : Matches any word character: [a-zA-Z0-9_].

  • \W : Matches any character not considered a word character.


Quantifiers

  • * - Matches 0 or more repetitions of the preceding pattern.

    Example: ab* matches “a”, “ab”, “abb”, “abbb”…

  • + - Matches 1 or more occurrences.

    Example: ab+ matches “ab”, “abb”, “abbb”… but not “a”.

  • ? - Matches 0 or 1 occurrence, making it optional.

    Example: ab? matches “a” or “ab”.

  • {n} - Exactly n occurrences.

    Example: a{3} matches “aaa”.

  • {n,} - At least n occurrences.

    Example: a{2,} matches “aa”, “aaa”, “aaaa”…

  • {n,m} - Between n and m occurrences.

    Example: a{2,4} matches “aa”, “aaa”, or “aaaa”.


Groups and Lookarounds

  • (abc) : Capturing group that matches “abc” and stores it.

    Example: (ha)+ matches “ha”, “hahaha”, etc.


  • (?:abc) : Non-capturing group; groups without saving.

    Useful when applying quantifiers or alternations without backreferences.


  • (?=abc) : Positive lookahead; matches if abc follows.

    Example: \d(?=px) matches a digit followed by “px”.


  • (?!abc) : Negative lookahead; matches if abc does not follow.

    Example: \d(?!px) matches digits not followed by “px”.


  • (?<=abc) : Positive lookbehind; matches if preceded by abc.

    Example: (?<=@)\w+ matches text after “@” in an email.


  • (?<!abc) : Negative lookbehind; matches if not preceded by abc.


Anchors and Boundaries

  • \b : Word boundary (between \w and \W).

    Example: \bcat\b matches “cat” in “the cat sat” but not “catering”.


  • \B : Non-word boundary.

    Example: \Bend matches “bend” but not “end”.


  • \A : Matches the start of the string (unlike ^, it doesn’t change with re.MULTILINE).

  • \Z : Matches the end of the string or before the newline at the end.

  • \z : Matches the absolute end of the string (rare in Python, often replaced with \Z).


Flags

You can pass flags to functions like re.search() or use them inline with (?i), (?m), etc.

  • re.IGNORECASE / re.I : Case-insensitive matching

  • re.MULTILINE / re.M : ^ and $ match line start/end

  • re.DOTALL / re.S : . matches newlines

  • re.VERBOSE / re.X : Allow whitespace and comments in the regex

  • re.ASCII / re.A : Restrict \w, \d, etc., to ASCII (not Unicode)


Python Regular Expressions Examples


Example 1: Email Validation

Try the Email RegEx Python Validator and Email Generator to test this pattern interactively.

import re
email_pattern = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
email = "user@example.com"
print("Email Valid:", bool(email_pattern.match(email)))

Example 2: Password Strength Check

Use the Password RegEx Python Validator or generate test data with our Password Generator

password_pattern = re.compile(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
password = "Aa123456!"
print("Password Strong:", bool(password_pattern.match(password)))

Example 3: Extracting Words from a String

Useful for NLP, logs, or data pipelines

text = "Regex is #1 at pattern matching!"
word_pattern = re.compile(r'\b\w+\b')
for match in word_pattern.finditer(text):
    print("Found:", match.group())


How It Works

  1. Enter your regex pattern and sample test string.

  2. View instant matches and capture groups below.

  3. Copy, edit, and refine your pattern until it’s perfect.

  4. Use test data from other tools to simulate real-world cases.


Pro Tips for Writing Effective RegEx in Python

  • Compile regex using re.compile() for better performance in loops.

  • Use named groups ((?P<name>...)) for cleaner, readable code.

  • Use re.VERBOSE for large regex, allowing comments and spacing.

  • Leverage re.findall() to return all matches as a list.

  • Avoid regex for deeply nested or structured data—use parsers instead.

  • Use Qodex’s Python Regex Tester to experiment with edge cases and live data.

  • Try generating test data with our Email Generator, UUID Generator, or Password Generator.

  • Combine regex with list comprehensions for powerful one-liners.

  • When debugging, test sections of complex patterns separately before joining them.


Tools for Enhanced RegEx Workflows


Other Regex Validators

Generators for Testing

Encoders & Decoders

Explore More on Qodex

Use Qodex to test and validate patterns in multiple languages

Frequently asked questions

How do I write regex in Python?×
Use the re module with functions like re.search(), re.match(), or re.findall().
How do I make regex case-insensitive in Python?+
How do I validate an email with regex in Python?+
What’s the difference between match() and search()?+
Where can I test my Python regex patterns?+