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.
The pattern
//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
(\w[\w.+-]*)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later reference\wMatches a word character (letter, digit, or underscore)[\w.+-]Character class — matches one character from: a word character (letter, digit, or underscore), '.', '+', '-'*The preceding element matches zero or more times (greedy)@Matches the character '@' literally(\w[\w-]*(?:\.\w[\w-]*)*\.[A-Za-z]{2,})Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference\wMatches a word character (letter, digit, or underscore)[\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'*The preceding element matches zero or more times (greedy)(?:\.\w[\w-]*)Non-capturing group — groups the pattern without capturing\.Matches the character '.' literally\wMatches a word character (letter, digit, or underscore)[\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'*The preceding element matches zero or more times (greedy)*The preceding element matches zero or more times (greedy)\.Matches the character '.' literally[A-Za-z]Character class — matches one character from: 'A' through 'Z', 'a' through 'z'{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());
}
}