RegEx : Find Email Addresses in a File using Grep

Here is a best regular expression that will help you to perform a validation and to extract all matched email addresses from a file.

This regular expression matches 99% of the email addresses in use nowadays.

In this article you'll find a regular expression itself and an example of how to extract matched email addresses from a file with the grep command.

Regular Expression to Match Email Addresses

Use the following regular expression to find and validate the email addresses :

Get a List of all Email Addresses with Grep

Execute the following command to extract a list of all email addresses from a given file :
$ grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt

Option Description
-E, --extended-regexp Use extended regular expression
-o, --only-matching Print email addresses only