Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • 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
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"([0-9]{3})([0-9]{3})(G|M|R|T{1})([0-9]{1,4}|[0-9]{1,3}K{0,1})(-)([0-9]{1,4}|[0-9]{1,3}K{0,1})(CM|FT|HM|HF|IN|KF|KM|MM|M|NM|SM|YD{1,2})") test_str = ("//Good Data\n" "123345T1234-5678KM\n" "000001T1234-5678KM\n" "001001T1234-5678KM\n" "011001T1234-567KM\n" "011011T1234-567KM\n" "011111T1234-567KM\n" "123345T123-5678KM\n" "123345T123-567KM\n" "123345T123-56KM\n" "123345T12-56KM\n" "123345T12-5KM\n" "123345T1-5KM\n" "123345T1234-567KM\n" "123345T1234-567HF\n" "123345T1234-567SM\n" "123345T123-5678KM\n" "123345T1234-567KKM\n" "123345T123-567KKM\n" "123345T123-567KHF\n" "123345T123-567KSM\n" "123345T123-56KKM\n" "123345T12-56KKM\n" "123345T12-5KKM\n" "123345T12-5KKM\n" "123345T1-5KKM\n" "000359T1234-5678KM\n" "123345T123K-5678KM\n" "123345T12K-5678KM\n" "123345T6K-5678KM\n" "123345T123K-567KKM\n" "123345T12K-56KKM\n" "123345T6K-5KKM\n" "123345T1234-5678M\n\n" "//Bad Data \n" "1234567T1234-5678KM\n" "12345T1234-5678KM\n" "1245T1234-5678KM\n" "145T1234-5678KM\n" "14T1234-5678KM\n" "1T1234-5678KM\n" "123360T1234-5678KM\n" "123345V1234-5678KM\n" "123345T12345-5678KM\n" "123345T1234-56789KM\n" "123345T-5678KM\n" "123345T123-KM\n" "123345T123-56KMK\n" "123345T1234-56KMK\n" "123345T12-5678KKM\n" "123345T1-5\n" "123345T1234567KM\n" "123345T1235678KKM\n" "123345!56T123-567KKM\n" "123!345T123-567KKM\n" "123345T12!3-56KKM\n" "123345T12-5!6KKM\n" "123345T12-5K!KM\n" "1233457T1234-5678K\n" "123345T1234-5678GH\n" "123345T1234-5678MK\n" "123345T1234-5678UUUU\n" " \n" " null\n" " \n" "000359T1234K-5678KM\n" "123345TK-5678KM\n" "123345T1234--5678KM\n\n\n\n") match = regex.search(test_str) if match: print(f"Match 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