Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
Write a regular expression that can replace all emails in text with "[EMAIL]":


"Christian has christian@email.com. Olof has the email olof@gmail.com. Lars has the emai britt123@oxford.co.uk."

How should i do this?
Posted
Updated 5-Mar-14 2:56am
v2
Comments
phil.o 5-Mar-14 8:56am    
What have you tried? Where are you stuck?

Try this:
C#
string textWithEmails = "Christian has christian@email.com. Olof has the email olof@gmail.com. Lars has the emai britt123@oxford.co.uk.";
string filteredStr = Regex.Replace(textWithEmails, @"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", "[EMAIL]");
Console.WriteLine(filteredStr);

The regular expression that I used is the email regex from the Regex Library of Expresso[^]
It is a rather complete regex, but not 100% complete. For a complete email regex, have a look here:
http://ex-parrot.com/~pdw/Mail-RFC822-Address.html[^]
 
Share this answer
 
v3
Comments
BillWoodruff 5-Mar-14 19:34pm    
+5
Thomas Daniels 6-Mar-14 4:50am    
Thank you!
It's pretty simple, See the MSDN[^] documentation.

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900