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

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

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 Python, please visit: https://docs.python.org/3/library/re.html