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

/
/
gm

Test String

Code Generator

Generated Code

import Foundation let pattern = #"^Cisco Catalyst.*AC Power Supply$"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = #""" Cisco Catalyst 9500 Series Chassis Cisco Catalyst 9500 Series Power Supply Bay Module Container Cisco Catalyst 9500 Series Power Supply Bay Module Container Cisco Catalyst 9500 Series Fan Tray Bay Module Container Cisco Catalyst 9500 Series 650W AC Power Supply Cisco Catalyst 9500 Series Power Supply Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series 650W AC Power Supply Cisco Catalyst 9500 Series Power Supply Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Tray Cisco Catalyst 9500 Series Fan Tray Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Fan Cisco Catalyst 9500 Series Router PSOC-MB_0: VOU PSOC-MB_1: VOU PSOC-MB_2: VOU PSOC-MB_3: VOU PSOC-MB_4: VOU PSOC-MB_5: VOU PSOC-MB_7: VOU PSOC-MB_8: VOU PSOC-MB_9: VOU PSOC-MB_10: VO 3570MB1_0: VOU 3570MB2_0: VOU 3570MB3_0: VOU 35215MB1_0: VO 35215MB2_0: VO Temp: Outlet_A Temp: Outlet_B Temp: Inlet_A Temp: Inlet_B Temp: UADP_0_0 Temp: UADP_0_1 Temp: UADP_0_2 Temp: UADP_0_3 Temp: UADP_0_4 Temp: UADP_0_5 Temp: UADP_0_6 Temp: UADP_0_7 Temp: UADP_0_8 PSOC-DB_1: VOU PSOC-DB_2: VOU PSOC-DB_4: VOU PSOC-DB_5: VOU PSOC-DB_7: VOU PSOC-DB_8: VOU PSOC-DB_11: VO 3570DB1_0: VOU 3570DB2_0: VOU 3570DB3_0: VOU Temp: Coretemp Temp: OutletDB Intel CPU x86-64 USB Port TwentyFiveGigE1/0/1 Container TwentyFiveGigE1/0/2 Container TwentyFiveGigE1/0/3 Container TwentyFiveGigE1/0/4 Container TwentyFiveGigE1/0/5 Container TwentyFiveGigE1/0/6 Container TwentyFiveGigE1/0/7 Container TwentyFiveGigE1/0/8 Container TwentyFiveGigE1/0/9 Container TwentyFiveGigE1/0/10 Container TwentyFiveGigE1/0/11 Container TwentyFiveGigE1/0/12 Container TwentyFiveGigE1/0/13 Container TwentyFiveGigE1/0/14 Container TwentyFiveGigE1/0/15 Container TwentyFiveGigE1/0/16 Container TwentyFiveGigE1/0/17 Container TwentyFiveGigE1/0/18 Container TwentyFiveGigE1/0/19 Container TwentyFiveGigE1/0/20 Container TwentyFiveGigE1/0/21 Container TwentyFiveGigE1/0/22 Container TwentyFiveGigE1/0/23 Container TwentyFiveGigE1/0/24 Container HundredGigE1/0/25 Container HundredGigE1/0/26 Container HundredGigE1/0/27 Container HundredGigE1/0/28 Container SFP+ 10GBASE-SR Twe1/0/1 Module Temperature Sensor Twe1/0/1 Supply Voltage Sensor Twe1/0/1 Bias Current Sensor Twe1/0/1 Transmit Power Sensor Twe1/0/1 Receive Power Sensor GE T GE T SFP+ 10GBASE-SR """# 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