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
No Match

/
/
g

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 = "(@.*?:[^\\}\\{@]+(\\{(?:[^\\}\\{]+|\\{(?:[^\\}\\{]+|\\{[^\\}\\{]*\\})*\\})*\\}))"; final String string = "CSS @media selector\n" + ".CSS Code sample test\n" + "@media screen and (min-width:0) and (max-width:640px) {\n" + " body.overlap .page-title .wf-wrap {\n" + " padding-top: 0!important;\n" + " padding-bottom: 4px!important\n" + " }\n" + " .h3-size {\n" + " font: normal 17px / 21px \"Roboto\",Helvetica,Arial,Verdana,sans-serif!important\n" + " }\n" + " body.breadcrumbs.breadcrumbs-bg {\n" + " margin-top: 0!important\n" + " }\n" + " .page-title .breadcrumbs,.page-title .breadcrumbs a {\n" + " font-size: 11px!important\n" + " }\n" + "}\n" + "@-webkit-keyframes fadeIn {0% { opacity: 0; }100% { opacity: 1; }}\n\n" + "@media screen and (max-width:200px){.sidebar-content {margin-bottom:20px}\n" + ".cat-item {padding:7px}\n" + ".widget_1 {padding-right:0}\n" + "}\n" + "/* TEST NESTED @media: */\n" + "@media screen and (max-width:200px){.sidebar-content {margin-bottom:20px}\n" + ".cat-item {padding:7px}\n" + "/* TEST NESTED @media capturing everything, comments too */\n" + "@media screen and (max-width:359px){.sidebar-content {margin-bottom:20px}\n" + ".cat-item {padding:7px}\n" + ".widget_1 {padding-right:0}\n" + "}\n" + ".widget_1 {padding-right:0}\n" + "}\n" + "@font-face { font-family: dashicons; src: url(\"https://emd4.com/\"); }\n" + ".h3-size {\n" + " font-style: normal\n" + "}\n" + "@media(min-width:768px){.vc_col-sm-1,.vc_col-sm-10,.vc_col-sm-11,.vc_col-sm-12,.vc_col-sm-2,.vc_col-sm-3,.vc_col-sm-4,.vc_col-sm-5,.vc_col-sm-6,.vc_col-sm-7,.vc_col-sm-8,.vc_col-sm-9{float:left}\n" + ".vc_col-sm-12{width:100%!important}\n" + ".vc_col-sm-11{width:91.66666667%}\n" + ".vc_col-sm-10{width:83.33333333%}\n" + ".vc_col-sm-9{width:75%}\n" + ".vc_col-sm-8{width:66.66666667%}\n" + ".vc_col-sm-7{width:58.33333333%}\n" + ".vc_col-sm-6{width:50%}\n" + ".vc_col-sm-5{width:41.66666667%}\n" + ".vc_col-sm-4{width:33.33333333%}\n" + ".vc_col-sm-3{width:25%}\n" + ".vc_col-sm-2{width:16.66666667%}\n" + ".vc_col-sm-1{width:8.33333333%}\n" + ".vc_col-sm-pull-12{right:100%}\n" + ".vc_col-sm-pull-11{right:91.66666667%}\n" + ".vc_col-sm-pull-10{right:83.33333333%}\n" + ".vc_col-sm-pull-9{right:75%}\n" + ".vc_col-sm-pull-8{right:66.66666667%}\n" + ".vc_col-sm-pull-7{right:58.33333333%}\n" + ".vc_col-sm-pull-6{right:50%}\n" + ".vc_col-sm-pull-5{right:41.66666667%}\n" + ".vc_col-sm-pull-4{right:33.33333333%}\n" + ".vc_col-sm-pull-3{right:25%}\n" + ".vc_col-sm-pull-2{right:16.66666667%}\n" + ".vc_col-sm-pull-1{right:8.33333333%}\n" + ".vc_col-sm-pull-0{right:auto}\n" + ".vc_col-sm-push-12{left:100%}\n" + ".vc_col-sm-push-11{left:91.66666667%}\n" + ".vc_col-sm-push-10{left:83.33333333%}\n" + ".vc_col-sm-push-9{left:75%}\n" + ".vc_col-sm-push-8{left:66.66666667%}\n" + ".vc_col-sm-push-7{left:58.33333333%}\n" + ".vc_col-sm-push-6{left:50%}\n" + ".vc_col-sm-push-5{left:41.66666667%}\n" + ".vc_col-sm-push-4{left:33.33333333%}\n" + ".vc_col-sm-push-3{left:25%}\n" + ".vc_col-sm-push-2{left:16.66666667%}\n" + ".vc_col-sm-push-1{left:8.33333333%}\n" + ".vc_col-sm-push-0{left:auto}\n" + ".vc_col-sm-offset-12{margin-left:100%}\n" + ".vc_col-sm-offset-11{margin-left:91.66666667%}\n" + ".vc_col-sm-offset-10{margin-left:83.33333333%}\n" + ".vc_col-sm-offset-9{margin-left:75%}\n" + ".vc_col-sm-offset-8{margin-left:66.66666667%}\n" + ".vc_col-sm-offset-7{margin-left:58.33333333%}\n" + ".vc_col-sm-offset-6{margin-left:50%}\n" + ".vc_col-sm-offset-5{margin-left:41.66666667%}\n" + ".vc_col-sm-offset-4{margin-left:33.33333333%}\n" + ".vc_col-sm-offset-3{margin-left:25%}\n" + ".vc_col-sm-offset-2{margin-left:16.66666667%}\n" + ".vc_col-sm-offset-1{margin-left:8.33333333%}\n" + ".vc_col-sm-offset-0{margin-left:0}}"; final Pattern pattern = Pattern.compile(regex); 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