Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
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]
  • Character class intersection
    [\w&&[^\d]]
  • 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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(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?)", flags=re.MULTILINE | re.IGNORECASE) 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 = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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