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

/
/
gmi

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"(?:\sfrom\s+(?<from>[^\s*]+)[\s]*(?:(?:[\[\(.]?(?<fromh>.*?)?\.?\ *?[\[\(]?(?<fromip>(?:[0-9a-z]*?[\.\:]+[0-9a-z]*?)+)?[\)\]]+)?)(?=[\s;]+(?:by|with|via|id|for|;)\s*?)|\s+by\s+(?<byv>.*?)(?:\s+?\((?:[a-z\-\.]*?\s+)*?\[?(?<byip>(?:(?:[0-9a-z]*?[\.\:]+[0-9a-z]*?)+))*\]?\)?)?(?=[\s;]+(?:from|with|via|id|for|;)\s*?)|\s+via\s+(?<via>[a-z0-9\.\s]*?)(?=(?:\s*;|[\s]+(?:from|by|with|for|id|;))\s*?)|\swith\s+(?<protocol>(?:.|\s)*?)(?=(?:\s*;|[\s]+(?:from|by|via|for|id|;))\s*?)|\s+id\s+(?<id>[^\s;]+)(?=(?:\s*;|[\s]+(?:from|by|via|with|for|;))\s*?)|\s+for\s+<?(?<for>[^\s;>]+)>?(?=(?:\s*;|[\s]+(?:from|by|via|with|id|;))\s*?)|;\s+(?<date>[a-z]{3},\s+[0-9]{1,2}\s+[a-z]{3,4}\s+[0-9]{4}\s+[0-9]{2}\:[0-9]{2}\:[0-9]{2}\s+(?:-[0-9]{4}\s+)?(?:\([a-z]+\)|\+[0-9]{4}))?\n?)" test_str = ("Received: from VE1PR01MB5599.eurprd01.prod.exchangelabs.com \n" " (aze [2603:10a6:7:7c::43]) by HE1PR0102MB2714.eurprd01.prod.exchangelabs.com with HTTPS via HE1PR0402CA0054.EURPRD04.PROD.OUTLOOK.COM with toto; Thu, 9 Jan 2020 16:34:13 +0000\n\n" "Received: from relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1])\n" " by relay-out.ohc.cu (Postfix) with ESMTP id 69EA722DD\n" " for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:43 -0500 (CST)\n\n" "Received: from correo.patrimonio.ohc.cu (unknown [192.168.229.20])\n" " by relay-out.ohc.cu (Postfix) via hello with ESMTP id B83BA22F5\n" " for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:36 -0500 (CST)\n\n" "Received: from correoweb.patrimonio.ohc.cu (unknown [192.168.229.23])\n" " by correo.patrimonio.ohc.cu (Postfix) with ESMTPA id EC62A232A00A;\n" " Thu, 9 Jan 2020 11:39:53 -0500 (CST)\n\n" "Received: from VE1PR01MB5599.eurprd01.prod.exchangelabs.com\n" " (2603:10a6:7:7c::43) by HE1PR0102MB2714.eurprd01.prod.exchangelabs.com with HTTPS via HE1PR0402CA0054.EURPRD04.PROD.OUTLOOK.COM; Thu, 9 Jan 2020 16:34:13\n" " +0000\n\n" "Received: from VI1PR0102CA0029.eurprd01.prod.exchangelabs.com\n" " (2603:10a6:802::42) by VE1PR01MB5599.eurprd01.prod.exchangelabs.com\n" " (2603:10a6:803:11f::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2602.12; Thu, 9 Jan\n" " 2020 16:34:13 +0000\n\n" "Received: from DB5EUR01FT034.eop-EUR01.prod.protection.outlook.com\n" " (2a01:111:f400:7e02::203) by VI1PR0102CA0029.outlook.office365.com\n" " (2603:10a6:802::42) with Microsoft SMTP Server (version=TLS1_2,\n" " cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9 via Frontend\n" " Transport; Thu, 9 Jan 2020 16:34:13 +0000\n\n" "Received: from relay-out.ohc.cu (200.55.138.44) by\n" " DB5EUR01FT034.mail.protection.outlook.com (10.152.4.246) with Microsoft SMTP\n" " Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n" " 15.20.2623.9 via Frontend Transport; Thu, 9 Jan 2020 16:34:12 +0000\n\n" "Received: from relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1])\n" " by relay-out.ohc.cu (Postfix) with ESMTP id 69EA722DD\n" " for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:43 -0500 (CST)\n\n" "Received: from relay-out.ohc.cu ([127.0.0.1])\n" " by relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1]) (amavisd-new, port 10024)\n" " with ESMTP id 7CZku5Y59vGC for <some.email@some.domain>;\n" " Thu, 9 Jan 2020 11:29:38 -0500 (CST)\n\n" "Received: from correo.patrimonio.ohc.cu (unknown [192.168.229.20])\n" " by relay-out.ohc.cu (Postfix) with ESMTP id B83BA22F5\n" " for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:36 -0500 (CST)\n\n" "Received: from localhost (localhost.localdomain [127.0.0.1])\n" " by correo.patrimonio.ohc.cu (Postfix) with ESMTP id 65413232A001\n" " for <some.email@some.domain>; Thu, 9 Jan 2020 11:40:05 -0500 (CST)\n\n" "Received: from correo.patrimonio.ohc.cu ([127.0.0.1])\n" " by localhost (correo.patrimonio.ohc.cu [127.0.0.1]) (amavisd-new, port 10024)\n" " with ESMTP id hNMp-6lHHtzH for <some.email@some.domain>;\n" " Thu, 9 Jan 2020 11:40:05 -0500 (CST)\n\n" "Received: from correoweb.patrimonio.ohc.cu (unknown [192.168.229.23])\n" " by correo.patrimonio.ohc.cu (Postfix) with ESMTPA id EC62A232A00A;\n" " Thu, 9 Jan 2020 11:39:53 -0500 (CST)") matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE) 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