Regular Expressions 101

Save & Share

  • Current Version: 28
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to 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
There are currently no sponsors. Become a sponsor today!
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]
  • 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"(?xi)^(\n" r"(AT)?U[0-9]{8} | # Austria\n" r"(BE)?0[0-9]{9} | # Belgium\n" r"(BG)?[0-9]{9,10} | # Bulgaria\n" r"(HR)?[0-9]{11} | # Croatia\n" r"(CY)?[0-9]{8}L | # Cyprus\n" r"(CZ)?[0-9]{8,10} | # Czech Republic\n" r"(DE)?[0-9]{9} | # Germany\n" r"(DK)?[0-9]{8} | # Denmark\n" r"(EE)?[0-9]{9} | # Estonia\n" r"(EL|GR)?[0-9]{9} | # Greece\n" r"ES[A-Z][0-9]{7}(?:[0-9]|[A-Z]) | # Spain\n" r"(FI)?[0-9]{8} | # Finland\n" r"(FR)?[0-9A-Z]{2}[0-9]{9} | # France\n" r"(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3}) | # United Kingdom\n" r"(HU)?[0-9]{8} | # Hungary\n" r"(IE)?[0-9]S[0-9]{5}L | # Ireland\n" r"(IT)?[0-9]{11} | # Italy\n" r"(LT)?([0-9]{9}|[0-9]{12}) | # Lithuania\n" r"(LU)?[0-9]{8} | # Luxembourg\n" r"(LV)?[0-9]{11} | # Latvia\n" r"(MT)?[0-9]{8} | # Malta\n" r"(NL)?[0-9]{9}B[0-9]{2} | # Netherlands\n" r"(PL)?[0-9]{10} | # Poland\n" r"(PT)?[0-9]{9} | # Portugal\n" r"(RO)?[0-9]{2,10} | # Romania\n" r"(SE)?[0-9]{12} | # Sweden\n" r"(SI)?[0-9]{8} | # Slovenia\n" r"(SK)?[0-9]{10} # Slovakia\n" r")$"), flags=re.MULTILINE) test_str = ("ATU99999999\n" "BE0999999999\n" "BG999999999\n" "HR12345678901\n" "CY99999999L\n" "CZ12345678\n" "DK99999999\n" "EE123456789\n" "FI99999999\n" "FRXX999999999\n" "DE999999999\n" "EL123456789\n" "HU12345678\n" "IE1S23456L\n" "IE1234567T\n" "IE1234567TW\n" "IE1234567FA\n" "ESA12345678\n" "ESZ1234567E") 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