Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new 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

/
/
gm

Test String

Substitution

Processing...

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"(\w+)\s\w(\d+)((\s)\w(\d+)\])*$" test_str = ("# Circuit generated by QLib\n" "# Bernstein-Vazirani search\n" "# Secret= 1\n" ".qubit 11\n" "qubit x0 \n" "qubit x1 \n" "qubit x2 \n" "qubit x3 \n" "qubit x4 \n" "qubit x5 \n" "qubit x6 \n" "qubit x7 \n" "qubit x8 \n" "qubit x9 \n" "qubit y\n" "Prep0 y\n" ".begin\n" "X y\n" "H x0\n" "H x1\n" "H x2\n" "H x3\n" "H x4\n" "H x5\n" "H x6\n" "H x7\n" "H x8\n" "H x9\n" "H y\n" "CNOT x0 y\n" "H x0\n" "H x1\n" "H x2\n" "H x3\n" "H x4\n" "H x5\n" "H x6\n" "H x7\n" "H x8\n" "H x9\n" "H y\n" ".end\n\n" "# Circuit generated by QLib\n" "# QFT for 15 qubits\n" ".qubit 15\n\n" "qubit q0\n" "qubit q1\n" "qubit q2\n" "qubit q3\n" "qubit q4\n" "qubit q5\n" "qubit q6\n" "qubit q7\n" "qubit q8\n" "qubit q9\n" "qubit q10\n" "qubit q11\n" "qubit q12\n" "qubit q13\n" "qubit q14\n\n" ".begin\n" "H q0\n" "CP q1 q0 2\n" "CP q2 q0 1\n" "CP q3 q0 0.5\n" "CP q4 q0 0.25\n" "CP q5 q0 0.125\n" "CP q6 q0 0.0625\n" "CP q7 q0 0.03125\n" "CP q8 q0 0.015625\n" "CP q9 q0 0.0078125\n" "CP q10 q0 0.00390625\n" "CP q11 q0 0.00195312\n" "CP q12 q0 0.000976562\n" "CP q13 q0 0.000488281\n" "CP q14 q0 0.000244141\n" "H q1\n" "CP q2 q1 2\n" "CP q3 q1 1\n" "CP q4 q1 0.5\n" "CP q5 q1 0.25\n" "CP q6 q1 0.125\n" "CP q7 q1 0.0625\n" "CP q8 q1 0.03125\n" "CP q9 q1 0.015625\n" "CP q10 q1 0.0078125\n" "CP q11 q1 0.00390625\n" "CP q12 q1 0.00195312\n" "CP q13 q1 0.000976562\n" "CP q14 q1 0.000488281\n" "H q2\n" "CP q3 q2 2\n" "CP q4 q2 1\n" "CP q5 q2 0.5\n" "CP q6 q2 0.25\n" "CP q7 q2 0.125\n" "CP q8 q2 0.0625\n" "CP q9 q2 0.03125\n" "CP q10 q2 0.015625\n" "CP q11 q2 0.0078125\n" "CP q12 q2 0.00390625\n" "CP q13 q2 0.00195312\n" "CP q14 q2 0.000976562\n" "H q3\n" "CP q4 q3 2\n" "CP q5 q3 1\n" "CP q6 q3 0.5\n" "CP q7 q3 0.25\n" "CP q8 q3 0.125\n" "CP q9 q3 0.0625\n" "CP q10 q3 0.03125\n" "CP q11 q3 0.015625\n" "CP q12 q3 0.0078125\n" "CP q13 q3 0.00390625\n" "CP q14 q3 0.00195312\n" "H q4\n" "CP q5 q4 2\n" "CP q6 q4 1\n" "CP q7 q4 0.5\n" "CP q8 q4 0.25\n" "CP q9 q4 0.125\n" "CP q10 q4 0.0625\n" "CP q11 q4 0.03125\n" "CP q12 q4 0.015625\n" "CP q13 q4 0.0078125\n" "CP q14 q4 0.00390625\n" "H q5\n" "CP q6 q5 2\n" "CP q7 q5 1\n" "CP q8 q5 0.5\n" "CP q9 q5 0.25\n" "CP q10 q5 0.125\n" "CP q11 q5 0.0625\n" "CP q12 q5 0.03125\n" "CP q13 q5 0.015625\n" "CP q14 q5 0.0078125\n" "H q6\n" "CP q7 q6 2\n" "CP q8 q6 1\n" "CP q9 q6 0.5\n" "CP q10 q6 0.25\n" "CP q11 q6 0.125\n" "CP q12 q6 0.0625\n" "CP q13 q6 0.03125\n" "CP q14 q6 0.015625\n" "H q7\n" "CP q8 q7 2\n" "CP q9 q7 1\n" "CP q10 q7 0.5\n" "CP q11 q7 0.25\n" "CP q12 q7 0.125\n" "CP q13 q7 0.0625\n" "CP q14 q7 0.03125\n" "H q8\n" "CP q9 q8 2\n" "CP q10 q8 1\n" "CP q11 q8 0.5\n" "CP q12 q8 0.25\n" "CP q13 q8 0.125\n" "CP q14 q8 0.0625\n" "H q9\n" "CP q10 q9 2\n" "CP q11 q9 1\n" "CP q12 q9 0.5\n" "CP q13 q9 0.25\n" "CP q14 q9 0.125\n" "H q10\n" "CP q11 q10 2\n" "CP q12 q10 1\n" "CP q13 q10 0.5\n" "CP q14 q10 0.25\n" "H q11\n" "CP q12 q11 2\n" "CP q13 q11 1\n" "CP q14 q11 0.5\n" "H q12\n" "CP q13 q12 2\n" "CP q14 q12 1\n" "H q13\n" "CP q14 q13 2\n" "H q14\n" ".end\n\n") subst = "" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.MULTILINE) if result: print (result) # 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