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
Was it useful? Share this post with the world!

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

  1. Thnx!

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

    1. wow! Cool info. Im new at all this but I like this command! TY.

  3. 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”

  4. Может кто-нибудь подскажет реализацию, но для поиска номеров.
    Т.е. первые цифры должны начинаться например с 7, +7, 8, длина должна быть 10, 11 символов.

  5. С номерами есть 2 вот таких регулярных выражения:

    1) ^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$
    2) "/(?<!\w)(?:(?:(?:(?:\+?3)?8\W{0,5})?0\W{0,5})?[34569]\s?\d[^\w,;(\+]{0,5})?\d\W{0,5}\d\W{0,5}\d\W{0,5}\d\W{0,5}\d\W{0,5}\d\W{0,5}\d(?!(\W?\d))/x"
    

    Как бы их адаптировать под эту функцию, помогите пожалуйста.

  6. Lovely. Saved my life.

Leave a Reply