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 (created by Steven Smith) and Dan Applemans excellent Regular Expressions with .NET book.

Comments (2)

  1. Nice one Alex 😉

    Now, if you were using my cool regexSnippets tool:

    You could save that pattern for a rainy day 🙂

  2. Chris Burgess says:

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