Regular Expressions 101

Save & Share

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""" (?<Loop> (?<NewBlock> (?:(?: ^[\s]*\n | ^(?:(?!\/\*).)+\n | ^(?:(?!\/\*).)+ ))+ )? (?<OldBlock> (?: \/\*.+\*\/ | (?:\/\*.*\n) (?:(?: ^[\s]*\n | ^(?:(?!\*\/).)+\n | ^(?:(?!\*\/).)+ ))+ \*\/ ) )? )+ """, flags=re.MULTILINE | re.VERBOSE) test_str = (" .highlight\n" " {\n" " background: #ffffff\n" " }\n\n" " .highlight .c,\n" " .highlight .cm,\n" " .highlight .cp,\n" " .highlight .c1,\n" " .highlight .cs\n" " {\n" " color: #007400;\n" " }\n" " /* Comment, \n" " Comment.Multiline, \n" " Comment.Preproc, \n" " Comment.Single, \n" " Comment.Special\n" " */\n\n" " .highlight .k,\n" " .highlight .kc,\n" " .highlight .kd,\n" " .highlight .kp,\n" " .highlight .kr,\n" " .highlight .nb\n" " {\n" " color: #AA0D91;\n" " }\n" " /* Keyword,\n" " Keyword.Constant,\n" " Keyword.Declaration,\n" " Keyword.Pseudo,\n" " Name.Builtin */\n\n" " .highlight .kt\n" " {\n" " color: #3F6E74\n" " }\n\n" " /* Keyword.Reserved */\n" " .highlight .err\n" " {\n" " color: #a61717;\n" " }\n\n" " /* Error */\n" " .highlight .o\n" " {\n" " font-weight: bold\n" " }\n\n" " /* Operator */\n" " .highlight .gd\n" " {\n" " color: #000000;\n" " background-color: #fdd\n" " }\n\n" " /* Generic.Deleted */\n" " .highlight .gd .x\n" " {\n" " color: #000000;\n" " background-color: #faa\n" " }\n\n" " /* Generic.Deleted.Specific */\n" " .highlight .ge\n" " {\n" " font-style: italic\n" " }\n\n" " /* Generic.Emph */\n" " .highlight .gr\n" " {\n" " color: #a00\n" " }\n\n" " /* Generic.Error */\n" " .highlight .gh\n" " {\n" " color: #999\n" " }\n\n" " /* Generic.Heading */\n" " .highlight .gi\n" " {\n" " color: #000000;\n" " background-color: #dfd\n" " }\n\n" " /* Generic.Inserted */\n" " .highlight .gi .x\n" " {\n" " color: #000000;\n" " background-color: #afa\n" " }\n\n" " /* Generic.Inserted.Specific */\n" " .highlight .go\n" " {\n" " color: #888\n" " }\n\n" " /* Generic.Output */\n" " .highlight .gp\n" " {\n" " color: #555\n" " }\n\n" " /* Generic.Prompt */\n" " .highlight .gs\n" " {\n" " font-weight: bold\n" " }\n\n" " /* Generic.Strong */\n" " .highlight .gu\n" " {\n" " color: #aaa\n" " }\n\n" " /* Generic.Subheading */\n" " .highlight .gt\n" " {\n" " color: #a00\n" " }\n\n" " /* Generic.Traceback */\n" " .highlight .m,\n" " .highlight .mf,\n" " .highlight .mh,\n" " .highlight .mi,\n" " .highlight .mo\n" " {\n" " color: #1C00CF;\n" " }\n\n" " /* Literal.Number,\n" " Literal.Number.Float,\n" " Literal.Number.Hex,\n" " Literal.Number.Integer,\n" " Literal.Number.Oct */\n" " .highlight .s\n" " {\n" " color: #C41A16;\n" " }\n\n" " /* Literal.String */\n" " .highlight .na\n" " {\n" " color: teal\n" " }\n\n" " /* Name.Attribute */\n" " .highlight .nc\n" " {\n" " color: #5C2699\n" " }\n\n" " /* Name.Class */\n" " .highlight .no\n" " {\n" " color: teal\n" " }\n\n" " /* Name.Constant */\n" " .highlight .ni\n" " {\n" " color: purple\n" " }\n\n" " /* Name.Entity */\n" " .highlight .ne\n" " {\n" " color: #990000\n" " }\n\n" " /* Name.Exception */\n" " .highlight .nn\n" " {\n" " color: #555\n" " }\n\n" " /* Name.Namespace */\n" " .highlight .nt\n" " {\n" " color: navy\n" " }\n\n" " /* Name.Tag */\n" " .highlight .nv,\n" " .highlight .nf\n" " {\n" " color: #3F6E74\n" " }\n\n" " /* Name.Variable,\n" " Name.Function */\n" " .highlight .ow\n" " {\n" " font-weight: bold\n" " }\n\n" " /* Operator.Word */\n" " .highlight .w\n" " {\n" " color: #bbb\n" " }\n\n" " /* Text.Whitespace */\n" " .highlight .sb\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Backtick */\n" " .highlight .sc\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Char */\n" " .highlight .sd\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Doc */\n" " .highlight .s2\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Double */\n" " .highlight .se\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Escape */\n" " .highlight .sh\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Heredoc */\n" " .highlight .si\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Interpol */\n" " .highlight .sx\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Other */\n" " .highlight .sr\n" " {\n" " color: #009926\n" " }\n\n" " /* Literal.String.Regex */\n" " .highlight .s1\n" " {\n" " color: #d14\n" " }\n\n" " /* Literal.String.Single */\n" " .highlight .ss\n" " {\n" " color: #990073\n" " }\n\n" " /* Literal.String.Symbol */\n" " .highlight .bp\n" " {\n" " color: #999\n" " }\n\n" " /* Name.Builtin.Pseudo */\n" " .highlight .vc\n" " {\n" " color: teal\n" " }\n\n" " /* Name.Variable.Class */\n" " .highlight .vg\n" " {\n" " color: teal\n" " }\n\n" " /* Name.Variable.Global */\n" " .highlight .vi\n" " {\n" " color: teal\n" " }\n\n" " /* Name.Variable.Instance */\n" " .highlight .il\n" " {\n" " color: #099\n" " }\n\n" " /* Literal.Number.Integer.Long */\n" " *,*:before,*:after\n" " {\n" " -moz-box-sizing: border-box;\n" " -webkit-box-sizing: border-box;\n" " box-sizing: border-box") 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