Obfuscate Email

S

Shapper

Hello,

I need to find all emails in a String and replace "@" by AT and the "." by DOT:

1 - "(e-mail address removed)" would become "name AT domain DOT com".

2 - "(e-mail address removed)" would become "name.surname AT domain DOT com"

Note that only the domain dot is replaced. Does this make sense?

At the moment I have the following:

public static String Obfuscate(this String value) {

Regex expression = new Regex(@"\b(?<start>[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*)@(?<end>(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum))\b", RegexOptions.IgnoreCase);

String replace = String.Concat("${start}", "AT", "${end}");

return expression.Replace(value, @replace);

}


I am not replacing the dot in this example. How can I do this?

And is there a better way to do this?
 
A

Arne Vajhøj

Hello,

I need to find all emails in a String and replace "@" by AT and the "." by DOT:

1 - "(e-mail address removed)" would become "name AT domain DOT com".

2 - "(e-mail address removed)" would become "name.surname AT domain DOT com"

Note that only the domain dot is replaced. Does this make sense?

At the moment I have the following:

public static String Obfuscate(this String value) {

Regex expression = new Regex(@"\b(?<start>[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*)@(?<end>(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum))\b", RegexOptions.IgnoreCase);

String replace = String.Concat("${start}", "AT", "${end}");

return expression.Replace(value, @replace);

}


I am not replacing the dot in this example. How can I do this?

And is there a better way to do this?

My take:

private static readonly Regex re = new
Regex(@"\b(\w+(\.\w+)*)@(\w+\.\w+(\.\w+)*)\b", RegexOptions.Compiled);
public static String Obfuscate(this String s) {
string res = s;
foreach(Match m in re.Matches(s))
{
res = res.Replace(m.Groups[0].Value, m.Groups[1].Value
+ " AT " + m.Groups[3].Value.Replace(".", " DOT "));
}
return res;
}

Note that I am just using \w - you may want to or need to replace
it with something else.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top