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

const regex = /(?:\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?)/gmi; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?:\\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?)', 'gmi') const str = `Received: from VE1PR01MB5599.eurprd01.prod.exchangelabs.com (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 Received: from relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1]) by relay-out.ohc.cu (Postfix) with ESMTP id 69EA722DD for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:43 -0500 (CST) Received: from correo.patrimonio.ohc.cu (unknown [192.168.229.20]) by relay-out.ohc.cu (Postfix) via hello with ESMTP id B83BA22F5 for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:36 -0500 (CST) Received: from correoweb.patrimonio.ohc.cu (unknown [192.168.229.23]) by correo.patrimonio.ohc.cu (Postfix) with ESMTPA id EC62A232A00A; Thu, 9 Jan 2020 11:39:53 -0500 (CST) Received: from VE1PR01MB5599.eurprd01.prod.exchangelabs.com (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 +0000 Received: from VI1PR0102CA0029.eurprd01.prod.exchangelabs.com (2603:10a6:802::42) by VE1PR01MB5599.eurprd01.prod.exchangelabs.com (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 2020 16:34:13 +0000 Received: from DB5EUR01FT034.eop-EUR01.prod.protection.outlook.com (2a01:111:f400:7e02::203) by VI1PR0102CA0029.outlook.office365.com (2603:10a6:802::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9 via Frontend Transport; Thu, 9 Jan 2020 16:34:13 +0000 Received: from relay-out.ohc.cu (200.55.138.44) by DB5EUR01FT034.mail.protection.outlook.com (10.152.4.246) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.9 via Frontend Transport; Thu, 9 Jan 2020 16:34:12 +0000 Received: from relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1]) by relay-out.ohc.cu (Postfix) with ESMTP id 69EA722DD for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:43 -0500 (CST) Received: from relay-out.ohc.cu ([127.0.0.1]) by relay-in.ohc.cu (relay-in.ohc.cu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7CZku5Y59vGC for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:38 -0500 (CST) Received: from correo.patrimonio.ohc.cu (unknown [192.168.229.20]) by relay-out.ohc.cu (Postfix) with ESMTP id B83BA22F5 for <some.email@some.domain>; Thu, 9 Jan 2020 11:29:36 -0500 (CST) Received: from localhost (localhost.localdomain [127.0.0.1]) by correo.patrimonio.ohc.cu (Postfix) with ESMTP id 65413232A001 for <some.email@some.domain>; Thu, 9 Jan 2020 11:40:05 -0500 (CST) Received: from correo.patrimonio.ohc.cu ([127.0.0.1]) by localhost (correo.patrimonio.ohc.cu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hNMp-6lHHtzH for <some.email@some.domain>; Thu, 9 Jan 2020 11:40:05 -0500 (CST) Received: from correoweb.patrimonio.ohc.cu (unknown [192.168.229.23]) by correo.patrimonio.ohc.cu (Postfix) with ESMTPA id EC62A232A00A; Thu, 9 Jan 2020 11:39:53 -0500 (CST)`; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions