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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(B\d{2}\w{7})" test_str = ("\n" " User account\n" " Research\n" "Bestseller\n" "Watchlist\n" " Monitoring\n" " User List\n" " Dashboard\n" " Log out\n" " Feedback\n" " Add product(s)\n" "Filters\n" "Marketplace\n\n" "Category\n\n" "Title contains\n\n" "Title contains\n" "Tag equals\n\n" "Tag equals\n" "Brand equals\n\n" "Brand equals\n" "Seller equals\n\n" "Seller equals\n" "Buybox\n\n" "Buybox\n" " Amazon\n\n" " FBA\n\n" " FBM\n\n" "Per page\n\n" "Compare\n\n" "Total products: 20\n" "Previous 1 Next\n" " Fav. Image MP Title\n" "ASIN\n" "Brand Price Sales rank\n" "Category Buybox # Sellers # Reviews\n" "Rating LQI Options\n" " \n" "Sternenlichter Sternenhimmel 500 Leuchtpunkte\n\n" "B019HAY3C4\n\n" "Sternenlichter\n\n\n" "€ 12.99 \n" "128075\n" "Küche & Haushalt\n\n" "FBA\n\n" "FRIES Digital\n\n" "1 \n" "49\n\n" "4.1 90 \n" " \n" "Bentgo - Bento Box / Lunchbox inkl. Besteck (Lila)\n\n" "B00B78W282\n\n" "Bentgo\n\n\n" "€ 17.90 \n" "10093\n" "Küche & Haushalt\n\n" "FBA\n\n" "use-again\n\n" "3 \n" "99\n\n" "4.3 75 \n" " \n" "Bentgo - Bento Box / Lunchbox inkl. Besteck (Grün)\n\n" "B00B78WO3U\n\n" "Bentgo\n\n\n" "€ 17.90 \n" "11190\n" "Küche & Haushalt\n\n" "FBA\n\n" "use-again\n\n" "1 \n" "99\n\n" "4.3 75 \n" " \n" "Bentgo - Bento Box / Lunchbox inkl. Besteck (Blau)\n\n" "B00B78T8U2\n\n" "Bentgo\n\n\n" "€ 17.90 \n" "10560\n" "Küche & Haushalt\n\n" "FBA\n\n" "use-again\n\n" "2 \n" "99\n\n" "4.3 75 \n" " \n" "Bentgo - Bento Box / Lunchbox inkl. Besteck (Grau)\n\n" "B00B78UBO4\n\n" "Bentgo\n\n\n" "€ 17.90 \n" "2658\n" "Küche & Haushalt\n\n" "FBA\n\n" "use-again\n\n" "2 \n" "99\n\n" "4.3 75 \n" " \n" "Emsa 516943 3-teiliges Frischhaltedosenset, 1 Liter, Transparent/Blau, Clip & Close\n\n" "B004QGXKRO\n\n" "Emsa\n\n\n" "€ 11.79 \n" "433\n" "Küche & Haushalt\n\n" "Amazon\n\n" "40 \n" "297\n\n" "4.6 90 \n" " \n" "Meal Prep Haven Lebensmittelbehälter mit 2 Fächern und Deckeln für die Portionskontrolle, ...\n\n" "B011SUKOHE\n\n" "Meal Prep Haven\n\n" "box \n\n" "463636\n" "Küche & Haushalt\n\n" "1 \n" "25 \n" " \n" "Mikrowelle und spülmaschinenfest stapelbar Mahlzeit Prep Kunststoff Lebensmittel Behälter ...\n\n" "B0195971NU\n\n" "Superior\n\n" "box \n\n" "€ 16.49 \n" "15941\n" "Küche & Haushalt\n\n" "FBA\n\n" "K Master\n\n" "1 \n" "40\n\n" "3.9 65 \n" " \n" "Mikrowelle und spülmaschinenfest stapelbar Mahlzeit Prep Kunststoff Lebensmittel Behälter ...\n\n" "B01BCNFT3Q\n\n" "Superior\n\n" "box \n\n" "€ 16.89 \n" "11542\n" "Küche & Haushalt\n\n" "FBA\n\n" "K Master\n\n" "1 \n" "40\n\n" "3.9 65 \n" " \n" "Mikrowelle und spülmaschinenfest stapelbar Mahlzeit Prep Kunststoff Lebensmittel Behälter ...\n\n" "B017KNFDOM\n\n" "Superior\n\n" "box \n\n" "€ 16.89 \n" "3630\n" "Küche & Haushalt\n\n" "FBA\n\n" "K Master\n\n" "2 \n" "40\n\n" "3.9 55 \n" " \n" "Meal Prep Haven Lebensmittelbehälter mit 3 Fächern und Deckeln für die Portionskontrolle, ...\n\n" "B011SY4P9I\n\n" "Meal Prep Haven\n\n" "box \n\n" "46144\n" "Küche & Haushalt\n\n" "1 \n" "4\n\n" "3.3 40 \n" " \n" "XL Silikon Backmatte von Cookin´Active, BPA frei, Platinum Silikon, Lebensmittelecht, Anti ...\n\n" "B01AR1L0FU\n\n" "Cookin'Active\n\n\n" "€ 17.99 \n" "3234\n" "Küche & Haushalt\n\n" "FBA\n\n" "Flatland Trading\n\n" "1 \n" "35\n\n" "5 90 \n" " \n" "WENKO 4374120100 Wäschesortierer Air - 6 breite Fächer, 100 % Polypropylen, 30 x 120 x 30 ...\n\n" "B0034KYP1I\n\n" "Wenko\n\n" "hängeregal \n\n" "€ 9.99 \n" "2669\n" "Küche & Haushalt\n\n" "Amazon\n\n" "5 \n" "57\n\n" "4.2 65 \n" " \n" "WENKO 64004100 Multi-Organizer Butterfly - 10 Fächer, 100% Polypropylen, 15 x 127 x 30 cm, ...\n\n" "B00URLQRMW\n\n" "Wenko\n\n" "hängeregal \n\n" "€ 11.99 \n" "451701\n" "Küche & Haushalt\n\n" "Amazon\n\n" "4 \n" "45 \n" " \n" "Metaltex 751016 Hängeregal, 10 Fächer, 20 x 29 x 120 cm\n\n" "B0098UXD3O\n\n" "Metaltex\n\n" "hängeregal \n\n" "€ 14.07 \n" "357099\n" "Küche & Haushalt\n\n" "Amazon\n\n" "1 \n" "20 \n" " \n" "Chintz, Vintage-Stil gemustert, 10 Taschen für Schuh-Aufbewahrung\n\n" "B00KD875RU\n\n" "Lloyd Pascal\n\n" "hängeregal \n\n" "€ 9.49 \n" "1540117\n" "Küche & Haushalt\n\n" "FBA\n\n" "ramsdensdirect\n\n" "2 \n" "20 \n" " \n" "WENKO 4372030100 Multiorganizer Comfort - 10 Fächer, Kunststoff - PEVA, 15 x 122 x 30 cm, ...\n\n" "B001AI590W\n\n" "Wenko\n\n" "hängeregal \n\n" "€ 9.99 \n" "34405\n" "Küche & Haushalt\n\n" "Amazon\n\n" "3 \n" "11\n\n" "3.9 20 \n" "Delete selected products\n" "Previous 1 Next\n" "Copyright © 2016 AMALYTIX.COM") matches = re.finditer(regex, test_str) 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