Click here to Skip to main content
12,821,494 members (25,093 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 18 Jul 2003

Extracting EMail Addresses From a Document or String

, 18 Jul 2003
Rate this:
Please Sign up or sign in to vote.
This project shows how to extract email addresses from a document or string.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Download Links --> <!-- Add the rest of your HTML here -->


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


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 - thanks guys!
    mc = Regex.Matches(source, _ 
    Dim results(mc.Count - 1) As String
    For i = 0 To results.Length - 1
        results(i) = mc(i).Value

    Return results
End Function


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
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 ( Rob has been recognized as a Microsoft Most Valuable Professional (MVP) for his involvement in the developer community.

You may also be interested in...


Comments and Discussions

GeneralLegitimate Use Pin
Anonymous17-Sep-04 2:31
sussAnonymous17-Sep-04 2:31 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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