Alex Lowe's .NET Blog

Taking feedback to the next level....

Removing email addresses from a string/text......

Yeah, it sounds easy but for a RegEx dummy like me it wasn't that easy (thanks Scott). Anyway, I thought that maybe someone some where would find this useful so why not put it on my blog:

//create regex object and intialize with regex validator
Regex r =
new Regex(@"[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}");

//replace all matching instances of regex validator in string variable 'body' with brackets
string newText = r.Replace(body, "<>");

I also wanted to point everyone to a nice Regular Expression repository RegExpLib.com (created by Steven Smith) and Dan Applemans excellent Regular Expressions with .NET book.

Published Monday, March 03, 2003 8:54 PM by AlexLowe

Comments

 

Darren Neimke said:

Nice one Alex ;)

Now, if you were using my cool regexSnippets tool:

http://www.flws.com.au/showusyourcode/MarkItUp.RegexSnippets.zip

You could save that pattern for a rainy day :-)
March 3, 2003 6:27 PM
 

Chris Burgess said:

You might want to check out Chris Sells' RegExDesigner.Net tool, too.

http://www.sellsbrothers.com/tools/
March 4, 2003 5:17 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker