Regular Expressions 101

Save & Share

  • Regex Version: ver. 4
  • 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
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"[-]{14}[0-9]+[\s].+[\s]+.+(?:[\s]*)([\s\S]*)[\s]{3}[-]{14}[0-9]+[\r\n]" test_str = ("Return-Path: <crown@noflem.date>\n" "Delivered-To: me@finlaydag33k.nl\n" "Received: from web01.01d.eu\n" " by web01.01d.eu with LMTP\n" " id UKmwFM5cels9EAAAfNQSUA\n" " (envelope-from <crown@noflem.date>)\n" " for <me@finlaydag33k.nl>; Mon, 20 Aug 2018 08:16:46 +0200\n" "Return-path: <crown@noflem.date>\n" "Envelope-to: me@finlaydag33k.nl\n" "Delivery-date: Mon, 20 Aug 2018 08:16:46 +0200\n" "Received: from mail by web01.01d.eu with spam-scanned (Exim 4.91)\n" " (envelope-from <crown@noflem.date>)\n" " id 1frdUb-0001AC-CB\n" " for me@finlaydag33k.nl; Mon, 20 Aug 2018 08:16:46 +0200\n" "X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on web01.01d.eu\n" "X-Spam-Level: ****\n" "X-Spam-Status: No, score=4.5 required=5.0 tests=FROMSPACE,\n" " HTML_FONT_LOW_CONTRAST,HTML_MESSAGE,RDNS_NONE,T_REMOTE_IMAGE autolearn=no\n" " autolearn_force=no version=3.4.1\n" "Received: from [107.174.30.3] (helo=noflem.date)\n" " by web01.01d.eu with esmtp (Exim 4.91)\n" " (envelope-from <crown@noflem.date>)\n" " id 1frdUZ-00019H-Eg\n" " for me@finlaydag33k.nl; Mon, 20 Aug 2018 08:16:45 +0200\n" "From: \" Roderick\" <crown@noflem.date>\n" "Date: Mon, 20 Aug 2018 01:09:47 -0500\n" "MIME-Version: 1.0\n" "Subject: Are you a good pilot? Want to become one?\n" "To: <me@finlaydag33k.nl>\n" "Message-ID: <28mfPO16iDxTxnPJ_jT4Dib_DlNAQCDEUhRyp96JUTE.NNYAMLzX6-vdM1DDpnOXYdmLgAmcdkEMIV3WWEEeguk@noflem.date>\n" "Content-Type: multipart/alternative;\n" " boundary=\"------------008794531541259352341780\"\n" "ReverseDNS: No reverse DNS for mailserver at 107.174.30.3, +100 Spam score\n" "SPFCheck: Server passes SPF test, -30 Spam score\n" "SpamTally: Final spam score: 70\n" "X-Antivirus-Scanner: Clean mail though you should still use an Antivirus\n\n" "This is a multi-part message in MIME format.\n" "--------------008794531541259352341780\n" "Content-Type: text/plain; charset=ISO-8859-1\n" "Content-Transfer-Encoding: 7bit\n\n" "The Game All Flight Fans Have Been Waiting For Is Finally Here...\n" "<http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO>The World's Most Realstic Flight Sim\n" "<http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO>\n" "Watch The Amazing Video Demo Below:\n" "<http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO>\n" "==>Fly Over 200+ Different Aircraft==&gt;Real Controls Based on Real Cockpits!\n" "<http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO>==&gt;Land Anywhere Worldswide With 25,000 Airport!==&gt;Real World Instrument Behaviour==&gt;Changable, Unpredictable Real-Time Weather\n\n" "Fasten Your Seatbelts &amp; Prepare To Take Flight!\n" "<http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO>\n\n\n\n" "You can be asked to be removed by going here \n" "<http://www.noflem.date/jfoqqvn/Opj5_SFF_jRHYZccPt3Qp1g0ljHZloZNOyXDjb0JTw8XHhojerqEmyefNIioyBHQKsYQNOrq2wI5EtqOulbttbhi_irrfSUPvFP21McyI0PcCw4ItD5UtfSkITxQp6MW.ofeReaGt1-VCYIC44BM2xP6EAjTUR03vqsqWuMCFiMu>or write to us below\n\n" "<!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}-->265 S. Schoolhouse St. Boynton Beach, FL 33435ing\n\n\n" "--------------008794531541259352341780\n" "Content-Type: text/html; charset=ISO-8859-1\n" "Content-Transfer-Encoding: 7bit\n\n" "<html>\n" "<head>\n\n" "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n" "</head>\n" "<body>\n\n" "<center>\n\n\n" "<table font-size:=\"\" helvetica=\"\" style=\"box-sizing: inherit; color: rgb(119, 119, 119); font-family: \" width=\"600px\">\n\n\n" " <tbody style=\"box-sizing: inherit;\">\n\n\n" " <tr style=\"box-sizing: inherit;\">\n\n\n" " <td align=\"left\" style=\"box-sizing: inherit;\">&nbsp;\n\n\n" " <div style=\"box-sizing: inherit; padding: 8px; border-radius: 10px;\">\n\n\n" " <center style=\"box-sizing: inherit;\">\n\n\n" " <div style=\"box-sizing: inherit; font-size: 19px;\"><b style=\"box-sizing: inherit;\"><a href=\"http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" rel=\"nofollow\" style=\"box-sizing: inherit; text-decoration-line: none; color: red;\" target=\"_blank\">The Game All Flight Fans Have Been Waiting For Is Finally Here...</a><br style=\"box-sizing: inherit;\" />\n\n\n" " <a href=\"http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" rel=\"nofollow\" style=\"box-sizing: inherit; text-decoration-line: none; color: black;\" target=\"_blank\">The World&#39;s Most Realstic Flight Sim</a></b></div>\n\n\n" " </center>\n\n\n\n\n\n" " <hr style=\"box-sizing: inherit; width: 310px; border-width: 3px; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-color: rgb(0, 176, 0);\" />&nbsp;\n\n\n" " <center style=\"box-sizing: inherit; font-size: 22px; background-color: rgb(254, 253, 53);\"><strong style=\"box-sizing: inherit;\"><a href=\"http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" rel=\"nofollow\" style=\"box-sizing: inherit; text-decoration-line: none; color: rgb(65, 82, 73);\" target=\"_blank\"><span style=\"box-sizing: inherit; color: rgb(255, 0, 0);\">Watch The Amazing Video Demo Below:</span></a></strong></center>\n\n\n" " &nbsp;\n\n\n\n\n\n" " <div style=\"box-sizing: inherit; font-size: 19px;\">==&gt;Fly Over 200+ Different Aircraft<br style=\"box-sizing: inherit;\" />\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " ==&gt;Real Controls Based on Real Cockpits!<br style=\"box-sizing: inherit;\" />\n\n\n" " &nbsp;\n\n\n" " <center style=\"box-sizing: inherit;\"><a href=\"http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" rel=\"nofollow\" style=\"box-sizing: inherit; text-decoration-line: none; color: rgb(66, 133, 245);\" target=\"_blank\"><img alt=\"vp3flight\" src=\"http://www.noflem.date/plane_flight.jpg\" style=\"box-sizing: inherit;\" /></a></center>\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " ==&gt;Land Anywhere Worldswide With 25,000 Airport!<br style=\"box-sizing: inherit;\" />\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " ==&gt;Real World Instrument Behaviour<br style=\"box-sizing: inherit;\" />\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " ==&gt;Changable, Unpredictable Real-Time Weather</div>\n\n\n" " &nbsp;\n\n\n\n\n\n" " <center style=\"box-sizing: inherit;\">\n\n\n" " <div style=\"box-sizing: inherit; font-size: 19px; background-color: rgb(55, 102, 144); padding: 8px; border-radius: 15px;\"><span style=\"box-sizing: inherit; font-size: 22px;\"><b style=\"box-sizing: inherit;\"><a href=\"http://www.noflem.date/hkeja/swdecr31drvszty/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" rel=\"nofollow\" style=\"box-sizing: inherit; text-decoration-line: none; color: rgb(255, 255, 255);\" target=\"_blank\">Fasten Your Seatbelts &amp; Prepare To Take Flight!</a></b></span></div><br />\n" "<img src=\"http://www.noflem.date/Mfqs4im7f/uMiFCMuWqsqv30RUTjAE6Px2MB44CIYCV-1tGaeRefo/WM6pQxTIkSftU5DtI4wCcP0IycM12PFvPUSfrri_ihbttbluOqtE5Iw2qrONQYsKQHByoiINfeymEqrejohHX8wTJ0bjDXyONZolZHjl0g1pQ3tPccZYHRj_FFS_5jpO\" >\n\n\n" " </center>\n\n\n\n\n\n" " <hr style=\"box-sizing: inherit; width: 600px; border-width: 5px; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-color: rgb(255, 0, 0);\" /></div>\n\n\n\n\n\n" " <p style=\"text-align: center;\"><br style=\"box-sizing: inherit;\" />\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " <br style=\"box-sizing: inherit;\" />\n\n\n" " You can be asked to be removed by going <a href=\"http://www.noflem.date/jfoqqvn/Opj5_SFF_jRHYZccPt3Qp1g0ljHZloZNOyXDjb0JTw8XHhojerqEmyefNIioyBHQKsYQNOrq2wI5EtqOulbttbhi_irrfSUPvFP21McyI0PcCw4ItD5UtfSkITxQp6MW.ofeReaGt1-VCYIC44BM2xP6EAjTUR03vqsqWuMCFiMu\">here </a>or write to us below</p>\n\n\n\n\n\n" " <center style=\"box-sizing: inherit;\">\n\n\n" " <style type=\"text/css\"><!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}-->\n\n\n" " </style>\n\n\n" " <span 28=\"\" 361=\"\" 7648=\"\" 8619=\"\" 872=\"\" alton=\"\" arnold=\"\" ave.=\"\" boynton=\"\" chapel=\"\" colonial=\"\" ct=\"\" data-sheets-userformat=\"{\" data-sheets-value=\"{\" fl=\"\" ga=\"\" oh=\"\" pa=\"\" road=\"\" s.=\"\" schoolhouse=\"\" south=\"\" st.=\"\" style=\"font-size: 11pt; font-family: Calibri, Arial;\" textspin:265=\"\" va=\"\" w.=\"\" wall=\"\">265 S. Schoolhouse St. Boynton Beach, FL 33435</span><span style=\"color: rgb(255, 255, 255); font-size: 4px;\">ing</span></center>\n\n\n" " </td>\n\n\n" " </tr>\n\n\n" " </tbody>\n\n\n" "</table>\n\n\n" "</center>\n\n\n\n\n" "</body>\n" "</html>\n\n" "--------------008794531541259352341780--\n") 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