Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / Visual Basic
Article

Extracting EMail Addresses From a Document or String

Rate me:
Please Sign up or sign in to vote.
3.75/5 (17 votes)
18 Jul 2003 138.9K   1.4K   40   17
This project shows how to extract email addresses from a document or string.

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.

VB.NET
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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mushtaque Nizamani18-Sep-10 12:46
Mushtaque Nizamani18-Sep-10 12:46 
QuestionHow can I extract all emails of body email ?? Pin
kiquenet.com21-Oct-08 11:15
professionalkiquenet.com21-Oct-08 11:15 
GeneralIts amazing!! Pin
wmhp115-Apr-07 15:17
wmhp115-Apr-07 15:17 
Generalit helps to make utility to search for string in word file Pin
digitaldxb12-Mar-07 22:39
digitaldxb12-Mar-07 22:39 
GeneralJust what I was looking for! Pin
fredde_d26-Feb-07 2:14
fredde_d26-Feb-07 2:14 
Generalthanks a bunch Pin
kalai_venkatesh20-Jan-06 1:47
kalai_venkatesh20-Jan-06 1:47 
Hi,
Thanks a lot for your code.. I was facing the deadline of my oroject and breaking my head to find out a solution.. your code saved me my head..keep up the good work.. thanks a bunch

Warmest Regards,

Kalai
GeneralJust What I Was Looking For Pin
PeteOnSoftware7-Dec-05 9:19
PeteOnSoftware7-Dec-05 9:19 
GeneralLegitimate Use Pin
Anonymous17-Sep-04 1:31
Anonymous17-Sep-04 1:31 
GeneralGood code snippet Pin
Nish Nishant17-Sep-03 19:40
sitebuilderNish Nishant17-Sep-03 19:40 
GeneralMotivation for the article Pin
Rob Windsor22-Jul-03 15:56
Rob Windsor22-Jul-03 15:56 
GeneralRe: Motivation for the article Pin
Richard Day22-Jul-03 22:12
Richard Day22-Jul-03 22:12 
GeneralRe: Motivation for the article Pin
vsabhi5-Apr-10 6:04
vsabhi5-Apr-10 6:04 
QuestionLegitimate Uses? Pin
Richard Day22-Jul-03 5:43
Richard Day22-Jul-03 5:43 
AnswerRe: Legitimate Uses? Pin
Anonymous22-Jul-03 14:46
Anonymous22-Jul-03 14:46 
GeneralRe: Legitimate Uses? Pin
Richard Day22-Jul-03 22:06
Richard Day22-Jul-03 22:06 
AnswerRe: Legitimate Uses? Pin
Gavin Harriss14-Dec-05 6:40
Gavin Harriss14-Dec-05 6:40 
GeneralBoo, and perhaps.... hiss... Pin
Anonymous21-Jul-03 12:00
Anonymous21-Jul-03 12:00 

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.