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:

"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b"

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

4 Replies to “RegEx: Find Email Addresses in a File using Grep”

  1. This matches way more than 99%.
    On 10 million, it missed 3! That’s 99.99997%

  2. I tried to limit the number of characters before @ without success.
    For example :
    grep -E -o “\b[A-Za-z0-9._%+-]{0,4}@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b”
    but my results are cut !
    Example :
    myemail@mydomain.com => mail@mydomain.com
    Any idea ?

    1. It should be 1 char to 4 char right ?
      grep -E -o “\b[A-Za-z0-9._%+-]{1,4}@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b”

Leave a Reply