Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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 = #"^[A-Z\d]+\s+\d{4}-\d{2}-\d{2}\s+\w+(?:[ -]\w+)*\s+\S+\s+\S+\s+(\S+)"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = ##""" 2020-08-26 PILE OF LIFE HEALTH PRODUCTS LP Page 1 A/P Remittance Advice Direct Deposit 2020-08-26 Cheque # 11361 Vendor # 0828 HAIN CELESTIAL CANADA, ULC Invoice # Date Description Gross Disc Net =============================================================== 225299 2020-07-24 P2156678 7,610.52 .00 7,610.52 225839 2020-07-22 P2157105 7,826.28 .00 7,826.28 225969 2020-07-22 P2157106 8,760.59 .00 8,760.59 226384 2020-07-22 P2157104 42,274.76 .00 42,274.76 CR01BEPJ 2020-08-17 MULTI MCBS 4,470.06- .00 4,470.06- CR01BEXS 2020-08-24 MULTI MCBS 5,212.81- .00 5,212.81- DM20082311 2020-08-14 LIFESTYLE MARKETS NA 201.25- .00 201.25- DM20083583 2020-08-17 KARDISH FOOD FRANCHI 281.37- .00 281.37- DM20085965 2020-08-12 AVRILSUPERMARCHE-WAR 871.50- .00 871.50- DM20086678 2020-08-12 AVRILSUPERMARCHE-WAR 871.50- .00 871.50- DM20089459 2020-07-30 LOBLAWS 8.90- .00 8.90- DM20089500 2020-08-14 COUNTRY GROCER-CHASE 105.00- .00 105.00- ========================================== 54,449.76 .00 54,449.76 Printed on 2020-08-26 at 6:26 2020-04-23 PILE OF LIFE HEALTH PRODUCTS LP Page 1 A/P Remittance Advice Direct Deposit 2020-04-23 Cheque # 9699 Vendor # 0828 HAIN CELESTIAL CANADA, ULC Invoice # Date Description Gross Disc Net =================================================================================== 218124 2020-02-27 P2151168 2,253.44 .00 2,253.44 219021 2020-03-18 P2152030 35,242.65 .00 35,242.65 219216 2020-03-18 P2152031 8,306.81 .00 8,306.81 CR01BASW 2020-04-20 MULTI MCBS 5,278.05- .00 5,278.05- DM2004W450 2020-04-17 RETURNS WFM-GR-20589 124.63- .00 124.63- DM2004W828 2020-04-17 RETURNS WFM-GR-20589 266.09- .00 266.09- DM20042157 2020-04-07 AVRIL 871.50- .00 871.50- DM20043798 2020-04-07 COUNTRY GROCER 105.00- .00 105.00- DM20043892 2020-04-07 COUNTRY GROCER 105.00- .00 105.00- DM20048663 2020-04-07 AVRIL 871.50- .00 871.50- DM20049986 2020-04-02 LA MOISSON 258.69- .00 258.69- ========================================== 37,922.44 .00 37,922.44 Printed on 2020-04-23 at 13:13 """## 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