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]
  • 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"^((?<atu>ATU)|(?<be>BE)|(?<bg>BG)|(?<cy>CY)|(?<cz>CZ)|(?<dk>DK)|(?<de>DE)|(?<ee>EE)|(?<el>EL)|(?<es>ES)|(?<fi>FI)|(?<fr>FR)|(?<hr>HR)|(?<hu>HU)|(?<ie>IE)|(?<lt>LT)|(?<lu>LU)|(?<lv>LV)|(?<mt>MT)|(?<nl>NL)|(?<pl>PL)|(?<pt>PT)|(?<ro>RO)|(?<se>SE)|(?<si>SI)|(?<sk>SK)|(?<xi>XI))((?(atu)[0-9]{8})|(?(be)[01][0-9]{9})|(?(bg)[0-9]{9,10})|(?(cy)[A-Z0-9]{9})|(?(cz)[0-9]{8,10})|(?(dk)[0-9]{8})|(?(de)[0-9]{9})|(?(ee)[0-9]{9})|(?(el)[0-9]{9})|(?(es)[A-Z]{1}[0-9]{8})|(?(fi)[0-9]{8})(?(fr)[0-9A-Z]{2}[0-9]{9})|(?(hr)[0-9]{11})|(?(hu)[0-9]{8})|(?(ie)(?:[0-9]{7}[A-Z]{1,2}|[0-9][A-Z][0-9]{5}[A-Z]))|(?(lt)(?:[0-9]{9}|[0-9]{12}))|(?(lu)[0-9]{8})|(?(lv)[0-9]{11})(?(mt)[0-9]{8})(?(nl)[0-9]{9}B[0-9]{2})|(?(pl)[0-9]{10})|(?(pt)[0-9]{9})|(?(ro)[0-9]{2,10})|(?(se)[0-9]{12})|(?(si)[0-9]{8})|(?(sk)[0-9]{10})|(?(xi)(?:[0-9]{3}[0-9]{4}[0-9]{2}(?:[0-9]{3})?|[A-Z0-9]{5})))$", flags=re.MULTILINE) test_str = ("ATU99999999\n" "BE0999999999\n" "BE1999999999\n" "BG999999999\n" "BG9999999999\n" "CY99999999L\n" "CZ99999999\n" "CZ999999999\n" "CZ9999999999\n" "DE999999999\n" "DK99999999\n" "EE999999999\n" "EL999999999\n" "ESX12345678\n" "FI99999999\n" "FRXX999999999\n" "HR99999999999\n" "HU99999999\n" "IE9S99999L\n" "IE9999999WI\n" "LT999999999\n" "LT999999999999\n" "LU99999999\n" "LV99999999999\n" "MT99999999\n" "NL000099998B57\n" "PL9999999999\n" "PT999999999\n" "RO999999999\n" "SE999999999999\n" "SI99999999\n" "SK9999999999\n" "XI999999999\n" "XI999999999999\n" "XIGD999\n" "XIHA999") 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