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
No Match

/
/

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"Order Total:\nCDN\$ ([\d\.]+)" test_str = ("---------- Forwarded message ----------\n" "From: auto-confirm@amazon.ca <auto-confirm@amazon.ca>\n" "Date: Fri, Nov 6, 2015 at 11:24 AM\n" "Subject: Your Amazon.ca order of \"Cheetah Mounts ALAMLB...\"\n" "To: Charles Regan <charles.regan@gmail.com>\n\n\n" "Amazon.ca\n" "Your Orders\n" " | Your Account | Amazon.ca\n" "Order Confirmation\n" "Order #701-7756830-8577864\n" "Hello Charles Regan,\n" "Thank you for shopping with us. We’ll send a confirmation once your item has shipped. Your order details are indicated below. If you would like to view the status of your order or make any changes to it, please visit Your Orders on Amazon.ca.\n\n" "Your estimated delivery date is: \n" "Wednesday, November 18, 2015\n\n" "Your shipping speed: \n" "FREE Shipping\n\n" "Your Orders\n" "Your order will be sent to: \n" "CHARLES REGAN \n" "1419 CH. DE L'ETANG-DU-NORD \n" "ETANG-DU-NORD, Québec G4T3B9 \n" "Canada\n\n" "Order Details\n" "Order #701-7756830-8577864 \n" "Placed on Friday, November 6, 2015\n\n" "Cheetah Mounts ALAMLB Articulating Arm (20\" Extension) TV Wall Mount Bracket for 23-49 inch LCD, LED and Plasma Flat Screen TVs up to VESA 400x400 and 66lbs, with full Ballhead Tilt, Swivel, and Rotation Motion, Including a Twisted Veins 10' Braided High Speed with Ethernet HDMI Cable and a 6\" 3-Axis Magnetic Bubble Level \n" "Electronics \n" "Sold by SPN Imports and Services \n" "Condition: New\n" "Facebook\n" "Twitter\n" "Pinterest\n" "CDN$ 29.33\n" "Item Subtotal:\n" "CDN$ 29.33\n" "Shipping & Handling:\n" "CDN$ 0.00\n" "Estimated Tax (GST/HST):\n" "CDN$ 0.00\n" "Estimated Tax (PST/QST):\n" "CDN$ 0.00\n" "Order Total:\n" "CDN$ 29.33\n" "To learn more about ordering, go to Ordering from Amazon.ca.\n" "If you want more information or need more assistance, go to Help.\n\n" "We hope to see you again soon!\n" "Amazon.ca\n" "This email was sent from a notification-only address that cannot accept incoming email. Please do not reply to this message.\n\n" " \n" "Amazon.ca\n" "Vos commandes\n" " | Votre compte | Amazon.ca\n" "Confirmation de la commande\n" "Commande n° 701-7756830-8577864\n" "Bonjour Charles Regan,\n" "Merci d'avoir magasiné chez nous. Nous vous enverrons une confirmation une fois que votre article aura été expédié. Les détails de votre commande sont indiqués ci-dessous. Veuillez visiter la section Vos commandes sur Amazon.ca si vous souhaitez voir le statut de votre commande ou apporter des modifications à celle-ci.\n\n" "Votre date de livraison prévue est le : \n" "mercredi 18 novembre 2015\n\n" "Votre mode de livraison : \n" "Livraison GRATUITE\n\n" "Your Orders\n" "Votre commande sera expédiée à : \n" "CHARLES REGAN \n" "1419 CH. DE L'ETANG-DU-NORD \n" "ETANG-DU-NORD, Québec G4T3B9 \n" "Canada\n\n" "Détails de la commande\n" "Commande n° 701-7756830-8577864 \n" "Passée le vendredi 6 novembre 2015\n\n" "Cheetah Mounts ALAMLB Articulating Arm (20\" Extension) TV Wall Mount Bracket for 23-49 inch LCD, LED and Plasma Flat Screen TVs up to VESA 400x400 and 66lbs, with full Ballhead Tilt, Swivel, and Rotation Motion, Including a Twisted Veins 10' Braided High Speed with Ethernet HDMI Cable and a 6\" 3-Axis Magnetic Bubble Level \n" "Electronics \n" "Vendu par SPN Imports and Services \n" "Condition : New\n" "Facebook\n" "Twitter\n" "Pinterest\n" "CDN$ 29.33\n" "Sous-total article(s) :\n" "CDN$ 29.33\n" "Expédition et manutention :\n" "CDN$ 0.00\n" "Estimation de la TPS/TVH :\n" "CDN$ 0.00\n" "Estimation de la TVP/TVQ :\n" "CDN$ 0.00\n" "Total de la commande :\n" "CDN$ 29.33\n" "Pour en apprendre davantage à propos des commandes, consultez la page Commander sur Amazon.ca.\n" "Si vous souhaitez obtenir de plus amples informations ou si vous avez besoin d'assistance, consultez nos pages d'aide.\n\n" "Nous espérons vous revoir bientôt.\n" "Amazon.ca\n" "Ce courriel a été envoyé à partir d'une adresse de notification uniquement qui ne peut recevoir de courriels entrants. Veuillez ne pas répondre à ce message.") matches = re.search(regex, test_str) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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