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

/
/
g

Test String

Code Generator

Generated Code

import Foundation let pattern = #"(\d{2})\/(\d{2})\/(\d{2})\s+(\d{2}):(\d{2})\s+([\d]+)\s+([\d]+|)\s+(<I>|)(EXT|)([\w]+|)(\s+|)((\d{1})'(\d{2})|)(\s+|)((\d{2}):(\d{2})'(\d{2})\s+(.*) |)"# let regex = try! NSRegularExpression(pattern: pattern) let testString = #""" 01/02/16 09:37 1142 01 2286542PPPPPP9131874263 00:00'24 01/02/16 09:37 1204 48 2181 00:01'33 01/02/16 09:37 1221 33 <I>3342 0'00 00:01'25 TR 01/02/16 09:38 1232 02 2286542PPPPPP9082035024 00:00'12 01/02/16 09:39 1711 EXT1132 01/02/16 09:39 1232 01 2286542PPPPPP9082035024 00:00'02 01/02/16 09:39 5910 11 <I> 0'01 00:00'37 D0 01/02/16 09:39 1602 EXT1611 01/02/16 09:39 1332 EXT1204 01/02/16 09:40 1222 34 <I>3933 0'02 00:00'14 01/02/16 09:40 1232 EXT1393 01/02/16 09:41 1262 EXT1154 03/02/16 16:11 2181 EXT2421 03/02/16 16:13 2181 05 89509600795 00:00'47 03/02/16 16:13 2314 24 1242 00:00'08 03/02/16 15:07 2162 05 <I> 0'00 00:00'15 TR 03/02/16 15:09 2004 05 <I> 0'05 00:00'50 03/02/16 15:11 2004 05 <I> 0'38 00:00'00 NA 03/02/16 15:12 2004 06 <I> 0'11 00:00'26 03/02/16 15:16 2004 05 <I> 0'04 00:01'02 03/02/16 15:18 2181 17 <I>1396 0'04 00:12'23 03/02/16 15:20 2151 07 89020100016 00:00'17 03/02/16 15:21 2004 24 1132 00:00'24 TR 03/02/16 15:21 2004 05 <I> 0'05 00:01'37 03/02/16 15:21 2151 07 89509668727 00:00'22 03/02/16 15:23 2151 05 89020116661 00:00'20 01/02/16 09:31 5910 11 <I> 0'01 00:00'24 D0 01/02/16 09:31 1221 33 <I>4351 0'00 00:04'55 TR 01/02/16 09:31 5910 03 <I>89831514875 0'01 00:00'14 D0 01/02/16 09:32 1204 11 <I> 0'00 00:00'45 TR 01/02/16 09:32 1125 03 <I>89831514875 0'00 00:00'16 TR 01/02/16 09:32 5910 03 <I>89831514875 0'01 00:00'13 D0 01/02/16 09:32 5910 09 <I> 0'01 00:00'10 D0 """# 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