Regex to Extract Emails from Text
Need to pull email addresses out of a document, log file, or web page? A simple regex pattern handles this in seconds.
Quick Answer
Use the pattern [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} to extract emails from any text. Test it instantly at dotsapps.com/tools/developer/regex-tester.
The Best Regex Pattern for Email Extraction
This pattern catches the vast majority of real email addresses:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Here's what each part does:
- [a-zA-Z0-9._%+-]+ — Matches the part before the @ sign. Letters, numbers, dots, underscores, percent signs, plus signs, and hyphens.
- @ — Matches the literal @ symbol.
- [a-zA-Z0-9.-]+ — Matches the domain name. Letters, numbers, dots, and hyphens.
- \.[a-zA-Z]{2,} — Matches the top-level domain like .com, .org, or .io. Must be at least 2 letters after the last dot.
This pattern works for addresses like john.doe@company.com, user+tag@mail.co.uk, and admin@sub.domain.org.
How to Extract All Emails from a Document
To find every email in a block of text, you need the global (g) flag. Without it, the regex stops at the first match.
Open the regex tester at dotsapps.com. Paste the email pattern above. Turn on the g flag. Then paste your text into the test area. Every email address gets highlighted instantly.
This works with any text — HTML source code, CSV files, log files, plain text documents, or copied content from websites. The regex ignores everything that isn't an email and pulls out just the addresses.
Email Regex Edge Cases to Watch For
No email regex is 100% perfect. Here are edge cases to know about:
- New TLDs — Domains like .photography or .company work fine with the {2,} length rule.
- IP-based emails — Addresses like user@[192.168.1.1] are technically valid but extremely rare. The basic pattern won't catch these.
- Quoted local parts — "weird email"@domain.com is valid per the spec but almost never seen in practice.
- Trailing periods — The pattern might match a trailing dot in sentences like "Email me at bob@test.com." You may need to trim results.
For real-world use, the basic pattern catches 99%+ of actual email addresses. Don't over-engineer it unless you have specific requirements.
Using the Extracted Emails in Your Code
Once you've confirmed the pattern works in the tester, use it in your code:
JavaScript:
const emails = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g) || [];
Python:
emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
Both return an array of all email addresses found in the text. The g flag in JavaScript and findall in Python ensure you get every match, not just the first one.
How to Do It: Step-by-Step
- 1
Open the Regex Tester at dotsapps.com
- 2
Enter the email pattern: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
- 3
Enable the global (g) flag to find all matches
- 4
Paste the text you want to extract emails from
- 5
All email addresses are highlighted — copy them for use in your project
Frequently Asked Questions
What is the simplest regex for email?
The simplest useful pattern is \S+@\S+\.\S+ which matches any non-space characters around an @ and dot. It's not precise but catches most emails with minimal complexity.
Can regex perfectly validate all email addresses?
No. The full email spec (RFC 5322) is extremely complex and a perfect regex for it would be thousands of characters long. Use a practical pattern for extraction and validate by sending a confirmation email.
How do I extract emails from HTML source code?
The same regex works on HTML source code. Just paste the HTML into the tester. The pattern finds emails whether they're in href attributes, plain text, or JavaScript code within the page.
Does the email regex work with international domains?
The basic pattern works with internationalized domain names that use ASCII-compatible encoding. For domains with non-ASCII characters (like umlauts), you'd need to add Unicode character classes.
Ready to Try It?
Regex Tester is free, private, and works right in your browser. No sign-up needed.
Open Regex Tester