Regular Expressions 101

Save & Share

  • Save new Regex
    ctrl+s
  • Update 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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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]{2}\d{4}(?=\s\d+)"# let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines) let testString = #""" Return to Contents Streets Improvement Summary Capital Expenses (1,000s) Total Prior Years FY 2021 FY 2022 FY 2023 FY 2024 FY 2025 Years 6-10 Beyond 10 Yrs Ocotillo Road - Greenfield to Higley ST0540 66,519 332 12,413 53,774 - - - - - Baseline Road - Burk to Consolidated Canal ST0710 1,340 87 1,253 - - - - - - Baseline Road - Greenfield to Power ST0780 8,801 112 - - - - 8,689 - - Recker Road - Ocotillo to Chandler Heights ST0800 11,611 - - - - - - 11,611 - Hunt Highway - Val Vista to 164th ST0840 86,241 40 - - - - - - 86,201 Recker Road - Ray to SR 202 ST0960 3,940 455 3,485 - - - - - - Higley Road - Riggs to Stacey ST0980 20,403 1,973 - - - - - 18,430 - Ocotillo Road - 148th to Greenfield ST0990 20,317 - 5,568 14,749 - - - - - Elliot Road - Neely to Burk ST1050 4,060 - - - - - - - 4,060 Val Vista Drive - Appleby to Riggs ST1120 34,277 7,513 26,764 - - - - - - Lindsay Road - Queen Creek to Ocotillo ST1140 3,881 171 1,097 2,613 - - - - - Hunt Highway - Higley to Recker ST1150 9,438 - - - - - - 9,438 - Recker Road - Riggs to Hunt Highway ST1160 11,395 104 - - - - - 11,291 - Lindsay Road - Pecos to SR 202 ST1170 12,171 6,609 5,562 - - - - - - Warner Road - Power to East of Recker ST1180 14,752 - - - 14,752 - - - - Power Road - Guadalupe to Santan Freeway ST1200 15,140 - - - 15,140 - - - - Val Vista Drive - Riggs to Hunt Highway ST1270 7,779 - - - - - - 7,779 - Warner and Greenfield Intersection ST1300 12,367 - - - - - - - 12,367 Ray and Gilbert Intersection ST1310 8,536 - - - - - - 8,536 - Elliot and Gilbert Intersection ST1320 8,626 66 - - - - 8,560 - - Guadalupe and Val Vista Intersection ST1330 8,880 - - - - - - 8,880 - Guadalupe and Power Intersection ST1340 8,790 - - 8,790 - - - - - Elliot and Cooper Intersection ST1380 10,370 670 9,700 - - - - - - Elliot and Higley Intersection ST1390 7,552 - - - - - - 7,552 - Germann Road - Gilbert to Val Vista ST1450 27,429 4,385 23,044 - - - - - - Turn Lane Safety and Congestion Improvements ST1540 9,329 723 1,170 1,104 1,583 1,583 1,583 1,583 - Accessibility Upgrades in Public ROW ST1550 2,209 1,278 586 345 - - - - - Constellation Way - South of Coldwater ST1570 3,356 55 - - - - - 3,301 - Lindsay Road/SR 202 Traffic Interchange ST1580 18,151 7,658 10,493 - - - - - - Higley and Pecos Intersection ST1590 928 - - 928 - - - - - Infrastructure Imp at Railroad Crossings ST1600 2,545 57 2,488 - - - - - - Mesquite Street - Lindsay to Val Vista ST1620 6,107 - - - 6,107 - - - - Adora Trails Improvements ST1660 874 830 44 - - - - - - Val Vista Drive Reconstruction ST1690 7,405 491 6,914 - - - - - - Val Vista Drive - Eastern Canal Bridge Repair ST1710 1,055 150 905 - - - - - - Val Vista Lakes Improvements ST1720 48,195 480 64 12,147 8,860 10,276 16,368 - - Higley and Baseline Intersection ST1740 5,028 4,714 314 - - - - - - Guadalupe Road Improvements ST1770 9,920 - 1,750 8,170 - - - - - PM10 Paving ST1790 3,283 131 52 1,512 - - - 1,588 - Cooper Road - Encinas to Baseline ST1800 7,743 - - 7,743 - - - - - Pecos Road - Allen to Lindsay ST1820 2,962 - 342 2,620 - - - - - Higley Road - North of Elliot ST1830 1,523 - - 1,523 - - - - - Cooley Station Transit Center ST1840 10,306 - - - - 10,306 - - - Greenfield Road Right Turn Lane at Knox ST1850 613 - - 613 - - - - - Gilbert and Warner Intersection ST1860 11,493 178 53 11,262 - - - - - McQueen and Elliot Intersection ST1870 11,434 - - 11,434 - - - - - Lindsay and Guadalupe Intersection ST1880 12,608 - - - - 12,608 - - - Chandler Heights Improvements ST1890 3,000 - 3,000 - - - - - - McQueen and Guadalupe Intersection ST1910 12,369 - 2,987 9,382 - - - - - Val Vista and Ray Intersection ST1920 9,707 - - - - - - 9,707 - Power and Queen Creek Intersection ST1940 8,537 - - - - - - 8,537 - Market and Williams Field Interesction ST1960 813 48 39 - 726 - - - - Val Vista and Mercy Intersection ST1970 1,172 48 39 1,085 - - - - - Market and San Tan Village Intersection ST1980 1,342 55 35 - 1,252 - - - - """# 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