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

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

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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx