Regular Expressions 101

Save & Share

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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "(?<Loop>\n" + "(?<NewBlock>\n" + " (?:(?:\n" + " ^[\\s]*\\n\n" + " |\n" + " ^(?:(?!\\/\\*).)+\\n\n" + " |\n" + " ^(?:(?!\\/\\*).)+\n" + " ))+\n" + ")?\n" + "(?<OldBlock>\n" + " (?:\n" + " \\/\\*.+\\*\\/\n" + " |\n" + " (?:\\/\\*.*\\n)\n" + " (?:(?:\n" + " ^[\\s]*\\n\n" + " |\n" + " ^(?:(?!\\*\\/).)+\\n\n" + " |\n" + " ^(?:(?!\\*\\/).)+\n" + " ))+\n" + " \\*\\/\n" + ")\n" + ")?\n" + ")+"; final String string = " .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"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html