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 Foundation let pattern = #"(@.*?:[^}{@]+(\{(?:[^}{]+|\{(?:[^}{]+|\{[^}{]*\})*\})*\}))"# let regex = try! NSRegularExpression(pattern: pattern) let testString = #""" CSS @media selector .CSS Code sample test @media screen and (min-width:0) and (max-width:640px) { body.overlap .page-title .wf-wrap { padding-top: 0!important; padding-bottom: 4px!important } .h3-size { font: normal 17px / 21px "Roboto",Helvetica,Arial,Verdana,sans-serif!important } body.breadcrumbs.breadcrumbs-bg { margin-top: 0!important } .page-title .breadcrumbs,.page-title .breadcrumbs a { font-size: 11px!important } } @-webkit-keyframes fadeIn {0% { opacity: 0; }100% { opacity: 1; }} @media screen and (max-width:200px){.sidebar-content {margin-bottom:20px} .cat-item {padding:7px} .widget_1 {padding-right:0} } /* TEST NESTED @media: */ @media screen and (max-width:200px){.sidebar-content {margin-bottom:20px} .cat-item {padding:7px} /* TEST NESTED @media capturing everything, comments too */ @media screen and (max-width:359px){.sidebar-content {margin-bottom:20px} .cat-item {padding:7px} .widget_1 {padding-right:0} } .widget_1 {padding-right:0} } @font-face { font-family: dashicons; src: url("https://emd4.com/"); } .h3-size { font-style: normal } @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} .vc_col-sm-12{width:100%!important} .vc_col-sm-11{width:91.66666667%} .vc_col-sm-10{width:83.33333333%} .vc_col-sm-9{width:75%} .vc_col-sm-8{width:66.66666667%} .vc_col-sm-7{width:58.33333333%} .vc_col-sm-6{width:50%} .vc_col-sm-5{width:41.66666667%} .vc_col-sm-4{width:33.33333333%} .vc_col-sm-3{width:25%} .vc_col-sm-2{width:16.66666667%} .vc_col-sm-1{width:8.33333333%} .vc_col-sm-pull-12{right:100%} .vc_col-sm-pull-11{right:91.66666667%} .vc_col-sm-pull-10{right:83.33333333%} .vc_col-sm-pull-9{right:75%} .vc_col-sm-pull-8{right:66.66666667%} .vc_col-sm-pull-7{right:58.33333333%} .vc_col-sm-pull-6{right:50%} .vc_col-sm-pull-5{right:41.66666667%} .vc_col-sm-pull-4{right:33.33333333%} .vc_col-sm-pull-3{right:25%} .vc_col-sm-pull-2{right:16.66666667%} .vc_col-sm-pull-1{right:8.33333333%} .vc_col-sm-pull-0{right:auto} .vc_col-sm-push-12{left:100%} .vc_col-sm-push-11{left:91.66666667%} .vc_col-sm-push-10{left:83.33333333%} .vc_col-sm-push-9{left:75%} .vc_col-sm-push-8{left:66.66666667%} .vc_col-sm-push-7{left:58.33333333%} .vc_col-sm-push-6{left:50%} .vc_col-sm-push-5{left:41.66666667%} .vc_col-sm-push-4{left:33.33333333%} .vc_col-sm-push-3{left:25%} .vc_col-sm-push-2{left:16.66666667%} .vc_col-sm-push-1{left:8.33333333%} .vc_col-sm-push-0{left:auto} .vc_col-sm-offset-12{margin-left:100%} .vc_col-sm-offset-11{margin-left:91.66666667%} .vc_col-sm-offset-10{margin-left:83.33333333%} .vc_col-sm-offset-9{margin-left:75%} .vc_col-sm-offset-8{margin-left:66.66666667%} .vc_col-sm-offset-7{margin-left:58.33333333%} .vc_col-sm-offset-6{margin-left:50%} .vc_col-sm-offset-5{margin-left:41.66666667%} .vc_col-sm-offset-4{margin-left:33.33333333%} .vc_col-sm-offset-3{margin-left:25%} .vc_col-sm-offset-2{margin-left:16.66666667%} .vc_col-sm-offset-1{margin-left:8.33333333%} .vc_col-sm-offset-0{margin-left:0}} """# let stringRange = NSRange(location: 0, length: testString.utf16.count) let matches = regex.matches(in: testString, range: stringRange) var result: [[String]] = [] for match in matches { var groups: [String] = [] for rangeIndex in 1 ..< match.numberOfRanges { let nsRange = match.range(at: rangeIndex) guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue } let string = (testString as NSString).substring(with: nsRange) groups.append(string) } if !groups.isEmpty { result.append(groups) } } print(result)

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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression