5,446,823 members and growing! (17,413 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Extracting EMail Addresses From a Document or String

By Rob Windsor

This project shows how to extract email addresses from a document or string.
VB, Windows, .NET 1.0, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 18 Jul 2003
Updated: 18 Jul 2003
Views: 57,304
Bookmarked: 25 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 3.87 Rating: 3.38 out of 5
5 votes, 35.7%
1
0 votes, 0.0%
2
1 vote, 7.1%
3
2 votes, 14.3%
4
6 votes, 42.9%
5

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


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.
Occupation: Web Developer
Location: Canada Canada

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralIts amazing!!memberwmhp116:17 15 Apr '07  
Generalit helps to make utility to search for string in word filememberdigitaldxb23:39 12 Mar '07  
GeneralJust what I was looking for!memberfredde_d3:14 26 Feb '07  
Generalthanks a bunchmemberkalai_venkatesh2:47 20 Jan '06  
GeneralJust What I Was Looking FormemberGuru777710:19 7 Dec '05  
GeneralLegitimate UsesussAnonymous2:31 17 Sep '04  
GeneralGood code snippeteditorNishant S20:40 17 Sep '03  
GeneralMotivation for the articlememberRob Windsor16:56 22 Jul '03  
GeneralRe: Motivation for the articlememberRichard Day23:12 22 Jul '03  
GeneralLegitimate Uses?memberRichard Day6:43 22 Jul '03  
GeneralRe: Legitimate Uses?sussAnonymous15:46 22 Jul '03  
GeneralRe: Legitimate Uses?memberRichard Day23:06 22 Jul '03  
AnswerRe: Legitimate Uses?memberGavin Harriss7:40 14 Dec '05  
GeneralBoo, and perhaps.... hiss...sussAnonymous13:00 21 Jul '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jul 2003
Editor: Nishant Sivakumar
Copyright 2003 by Rob Windsor
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project