Click here to Skip to main content
Click here to Skip to main content

Extracting EMail Addresses From a Document or String

By , 18 Jul 2003
 

Introduction

This project shows how to extract email addresses from a document or string.

Background

I was listening to the most recent .NET Rocks where Carl Franklin mentioned an exercise he had in a class that asked the attendees to extract email addresses from a string. He said that the exercise took some people a couple hours to complete using VB 6.0 but I was just working with the System.Text.RegularExpressions namespace and I thought this would be quite easy in .NET.

Using the code

The sample application will open a Word Document, Rich Text Document, or Text File and give you all the email addresses contained within. It uses Word (late-bound so it's version independant) to open the .DOC or .RTF files.

The heart of the sample application is the method listed below. It uses the Regex.Matches method to search the string for matches to the regular expression provided. You then just need to enumerate the returned MatchCollection to extract the email addresses.

Perhaps the biggest challenge is to construct the proper regular expression for the search. I went to The Regular Expression Library to search for the one used here.

Imports System.Text.RegularExpressions
'.......................

Private Function ExtractEmailAddressesFromString(ByVal source As String) _
        As String()
    Dim mc As MatchCollection
    Dim i As Integer

    ' expression garnered from www.regexlib.com - thanks guys!
    mc = Regex.Matches(source, _ 
        "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})")
    Dim results(mc.Count - 1) As String
    For i = 0 To results.Length - 1
        results(i) = mc(i).Value
    Next

    Return results
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rob Windsor
Web Developer
Canada Canada
Member
Rob Windsor is an independent consultant and mentor based in Toronto, Canada. Rob focuses on the development of custom business applications using Microsoft technologies and is also an instructor for Learning Tree International where he teaches many of the courses in the .NET curriculum. Rob is a regular speaker at User Group meetings in the Toronto area and is President of the Toronto Visual Basic User Group (www.tvbug.com). Rob has been recognized as a Microsoft Most Valuable Professional (MVP) for his involvement in the developer community.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMotivation for the articlememberRob Windsor22 Jul '03 - 15:56 
This article was motivated by the piece of sample code listed which was in turn motivated by a part of a discussion on .NET RocksThe host, Carl Franklin, mentioned that he had given attendees to one of his courses an exercise that involved extracting email addresses from a flat file and that it took most of the people an hour or more to complete the task.
 
I had just been working with the classes in System.Text.RegularExpressions and read Dan Appleman’s eBook on Regular Expressions and it occurred to me that this would be almost trivial in .NET. So, as an exercise for myself and to prove my assumption correct I went about writing the routine presented in the article and then the sample application provided for download. The whole process took less than half-an-hour, most of which was consumed trying to find the appropriate search pattern for the call to Regex.Matches.
 
After completing this process I though someone else might find this work helpful or interesting so I decided to post it. I didn’t create the code to help someone in his or her spamming efforts. I hate Spam just as much as the next person, probably even more. I was just enthusiastic to find another area where .NET makes something easy that used to be much harder and I wanted to share what I’d found.

 
====
Rob Windsor
G6 Consulting
Toronto, Canada
GeneralRe: Motivation for the articlememberRichard Day22 Jul '03 - 22:12 
I don't have a problem with the article as such. It is a beginner article but that's not necessarily a problem. It was fairly well written and I assume in the code download is some info on accessing word docs which could be useful.
 
My concern was the subject matter. Perhaps extracting URL's or simlar may have been more appropriate.
 
However, thanks for your contribution to CodeProject.
GeneralRe: Motivation for the articlemembervsabhi5 Apr '10 - 6:04 
Hi. i have emailed you yesterday regarding this code. i was trying to get the email ids from all the files in a folder. how to modify your code so that it would read each file in a folder and extract email id and save all the emails to a word document or an excel sheet. any suggestion would be helpful

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Jul 2003
Article Copyright 2003 by Rob Windsor
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid