RegexCraft

Email regex

A practical email pattern: a name made of word characters, dots, plus and hyphen, then @, then a domain with at least one dot and a letter ending. Group 1 captures the name and group 2 the whole domain, including multi-part endings like .co.uk.

Open in the tester

The pattern

/(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})/g

Sample text, 2 matches

Reach Ada at ada@example.com or Grace at grace.lovelace@analytics.co.uk for the payload at https://status.example.io/health.

Point at a coloured group to see it in the pattern, the text and the explanation.

What it deliberately doesn’t do

  • It does not implement RFC 5322. Quoted names, comments and IP-literal domains are not matched.
  • It does not prove an address exists; only sending mail to it does.
  • Internationalised domains must be in their ASCII (xn--) form.

Explained, in order

  1. (\w[\w.+-]*)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later reference
  2. \wMatches a word character (letter, digit, or underscore)
  3. [\w.+-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '+', '-'
  4. *The preceding element matches zero or more times (greedy)
  5. @Matches the character '@' literally
  6. (\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference
  7. \wMatches a word character (letter, digit, or underscore)
  8. [\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'
  9. *The preceding element matches zero or more times (greedy)
  10. (?:\.\w[\w-]*)Non-capturing group — groups the pattern without capturing
  11. \.Matches the character '.' literally
  12. \wMatches a word character (letter, digit, or underscore)
  13. [\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'
  14. *The preceding element matches zero or more times (greedy)
  15. *The preceding element matches zero or more times (greedy)
  16. \.Matches the character '.' literally
  17. [A-Za-z]Character class — matches one character from: 'A' through 'Z', 'a' through 'z'
  18. {2,}The preceding element matches at least 2 times (greedy)

In your language

JavaScript

The engine this page was tested with.

const re = /(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})/g;

// Example
const matches = [...str.matchAll(/(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})/g)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

pattern = re.compile(r"(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})")

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

var re = regexp.MustCompile(`(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).

fn main() {
    let re = Regex::new(r#"(\w[\w.+-]*)@(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}