Regular Expressions 101

Save & Manage Regex

  • Current Version: 2
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Character class intersection
    [\w&&[^\d]]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^[a-z]{1}[a-z0-9!#$%&'*+-\/=?^_`{|}~]{0,}@[a-z]{1}[a-z.0-9-]{0,}[a-z0-9]{1}\.[a-z0-9]{2,}$", flags=re.IGNORECASE | re.MULTILINE) test_str = ("opinionated email validation based on https://en.wikipedia.org/wiki/Email_address\n\n" "Valid email addresses\n" "simple@example.com\n\n" "werw@mydomain.gov.us\n" "very.common@example.com\n" "very.co34mmon@example.com\n" "disposable.style.email.with+symbol@example.com\n" "other.email-with-hyphen@example.com\n" "other.email-with-hyphen@ex12ample.com\n" "other.email-with-hyphen@exa-mple.com\n" "fully-qualified-domain@example.com\n" "example-indeed@strange-example.com\n\n\n\n\n" "Invalid email addresses\n" "user-@example.org (local part ending with non-alphanumeric character from the list of allowed printable characters)\n" "x@example.com (one-letter local-part)\n" "admin@mailserver1 (local domain name with no TLD, although ICANN highly discourages dotless email addresses[10])\n" "user.name+tag+sorting@example.com (may go to user.name@example.com inbox depending on mail server)\n" "user%example.com@example.org (% escaped mail route to user@example.com via example.org)\n" "mailhost!username@example.org (bangified host route used for uucp mailers)\n\n" "example@s.example (see the List of Internet top-level domains)\n" " @example.org (space between the quotes)\n" "john..doe@example.org (quoted double dot)\n" "Abc.example.com (no @ character)\n" "A@b@c@example.com (only one @ is allowed outside quotation marks)\n" "a\"b(c)d,e:f;g<h>i[j\\k]l@example.com (none of the special characters in this local-part are allowed outside quotation marks)\n" "just\"not\"right@example.com (quoted strings must be dot separated or the only element making up the local-part)\n" "this is\"not\\allowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash)\n" "this\\ still\\\"not\\\\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)\n" "1234567890123456789012345678901234567890123456789012345678901234+x@example.com (local-part is longer than 64 characters)\n" "i_like_underscore@but_its_not_allowed_in_this_part.example.com (Underscore is not allowed in domain part)") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html