Regex to Extract All Email Addresses from a String βœ… (Tutorial + Online Tool)

I have just created this small helper tool to extract all email addresses from a huge text file or string. Simply copy and paste your text into the field and click “Extract Emails“: πŸ‘‡

Email Extractor

Extracted Emails:

JavaScript Regex

In the online tool above, I used a JavaScript regular expression:

var emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g; 

The variable emailRegex holds a regular expression, which is a pattern used to match character combinations in strings, specifically designed here to identify email addresses.

In the pattern /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g, [a-zA-Z0-9._%+-]+ matches the username part of the email, allowing for a combination of uppercase and lowercase letters, numbers, and several special characters.

The @ symbol is a literal match, ensuring the presence of the at-symbol in the email.

[a-zA-Z0-9.-]+ matches the domain name, allowing letters, numbers, periods, and hyphens. \. ensures a literal period is present, and [a-zA-Z]{2,} matches the top-level domain (like com, org, net), ensuring it is composed of at least two letters.

The g at the end of the pattern indicates a global search, meaning it will match all instances of the pattern in the provided string, not just the first one it encounters.

This regular expression provides a general pattern for identifying email addresses within a text, although it’s worth noting that validating email addresses using a regular expression can get exceedingly complex due to the wide variety of valid formats specified by the standard.

Regex to Match Email Addresses in Different Programming Languages

Fortunately, regular expressions (regex) tend to be quite consistent across different programming languages and environments, as many languages implement regular expressions in a way that adheres to a standard (often, the Perl 5 regular expression syntax). However, there can be slight variations or additional features in some languages.

Below is a general regex pattern for matching email addresses, along with examples of how it might be implemented in various programming languages:

General Regex Pattern:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

1. JavaScript:

var emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;

2. Python (using re module):

import re
email_regex = re.compile(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}')

You can also check out this Finxter article to learn more about scraping websites to extract email addresses. However, make sure you have the right to do so!

3. Java:

Pattern emailRegex = Pattern.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");

4. C# (.NET):

Regex emailRegex = new Regex(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}");

5. PHP (using preg_match):

$emailRegex = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';

6. Ruby:

email_regex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/

7. Swift:

let emailRegex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"

8. Kotlin:

val emailRegex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}".toRegex()

9. Perl:

my $email_regex = qr/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;

10. R:

email_regex <- "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"

Note that the above regex pattern is quite basic and will cover many, but not all, valid email address formats. Email validation can get quite complex if you want to cover all valid email formats as specified by the standards (RFC 5322, RFC 6531). The above examples should work for many common use cases, but be sure to test thoroughly with your own data to ensure no valid email addresses are being missed.

Have fun, thanks for reading, and keep learning! πŸš€

Feel free to join our free email newsletter to stay tuned about exciting tech news while learning coding and AI tools, as well as prompt engineering: