Regular Expressions 101

Save & Share

  • Regex Version: ver. 2
  • Fork Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

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

/
/
gmx

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r""" # Valid quoted metatags & groups must be followed by whitespace or the end of input to be valid; otherwise they are matched as a non-quoted metatag or standard tag respectively # Both quoted & non-quoted metatags must have a non-empty body (after leading and/or trailing `"` are removed) & a non-empty name; otherwise they are matched as a standard tag #/\G(?>\s*)(?<token> # Match any leading whitespace to help with \G # (?<prefix>[-~])? # (?<body> # (?<metatag>(?>\w+:(?>"[^\"]+"(?=\s|\z)|#{R_FRAG_TK})))| # Match a metatag (quoted or not) # (?<group>(?> # Match a single group atomically by: # (?>\(\s+) # 1. atomically matching a `(` & at least 1 whitespace character # (?<subquery>(?> # Greedily find one of the following 2 options # (?!(?<=\s)\)|(?>\s+)\)) # 2. Skip this option if a `)` that's preceded by whitespace is next # (?> # 3. Matching one of the following 3 options once: # [-~]?\g<metatag>| # 3A. a metatag (to avoid misinterpreting quoted input as groups) # [-~]?\g<group>| # 3B. a group (to balance parentheses) # (?> # 3C. Atomically match either: # [^\s)]+| # - 1 or more non-whitespace, non-`)` characters greedily, or # (?<!\s)\)+ # - If not preceded by whitespace, 1 or more `)` # )* # Match 3C 0 or more times greedily # ) # (?>(?>\s+)(?!\)))?| # 4. Atomically match all contiguous whitespace (if present). Or; # (?=(?<=\s)\)|(?>\s+)\)) # 5. Succeed if the prior char was whitespace and the next is a closing parenthesis. Backtracks the parenthesis. Takes advantage of special handling of zero-length matches. # )+) # If step 5 succeeds, the zero-length match will force the engine to stop trying to match this group. # (?>\s*)(?<=\s)\) # Check if preceded by whitespace and match the closing parenthesis. # )(?=\s|\z))| # (?<tag>\S+) # Match non-whitespace characters (tags) # ))(?>\s*)/x # Match any trailing whitespace to help with \G \G(?>\s*)(?<token>(?<prefix>[-~])?(?<body>(?<metatag>(?>\w+:(?>"[^\"]+"(?=\s|\z)|(?>"{0,2})(?!(?<=")(?=\s|\z))\S+)))|(?<group>(?>(?>\(\s+)(?<subquery>(?>(?!(?<=\s)\)|(?>\s+)\))(?>[-~]?\g<metatag>|[-~]?\g<group>|(?>[^\s)]+|(?<!\s)\)+)*)(?>(?>\s+)(?!\)))?|(?=(?<=\s)\)|(?>\s+)\)))+)(?>\s*)(?<=\s)\))(?=\s|\z))|(?<tag>\S+)))(?>\s*) """ test_str = "~( jun_kobayashi solo ) ~fav:Somebody ~( jacket_(hotline_miami) duo )" matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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