Obfuscate

S

shapper

Hello,

I need to obfuscate all emails in a string. e.g:

To replace an email "(e-mail address removed)" by "name(at)domain(dot).com".

I have been using the following but it only replaces @ and I am not
sure if this is the best way to do this:

public static String Obfuscate(String text, String at) {
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 fix = String.Concat("${start}", at, "${end}");
return text == null ? null : expression.Replace(text, @fix);
} // Obfuscate

I would like to be able to use replace the DOT to and maybe not using
a regex would be better?

Thanks,
Miguel
 
P

Peter Duniho

Hello,

I need to obfuscate all emails in a string. e.g:

To replace an email "(e-mail address removed)" by "name(at)domain(dot).com".

[...]
I would like to be able to use replace the DOT to and maybe not using
a regex would be better?

Seems to me you're overthinking the problem.

Assuming you don't need this method to be validating the email string or
anything like that, I'd just use the String.Replace() method:

public static String Obfuscate(String text)
{
return text.Replace("@", "(at)").Replace(".", "(dot)");
}

I don't see any need to parse the address itself. Just replace the
characters of interest and be done with it.

Pete
 
S

shapper

Assuming you don't need this method to be validating the email string or  
anything like that, I'd just use the String.Replace() method:

     public static String Obfuscate(String text)
     {
         return text.Replace("@", "(at)").Replace(".", "(dot)");
     }

I don't see any need to parse the address itself.  Just replace the  
characters of interest and be done with it.

And if the string contains text with 2 emails in it ... but in the
text dots and @'s are also used but not in email addresses?

What about:

public static string Obfuscate(string text)
{
foreach (string str in text.Split(' '))
{
if (IsEmailAddress(str.Trim()) == true)
{
text = text.Replace(str, str.Replace("@",
"(at)").Replace(".", "(dot)"));
}
}

return text;
}

public static bool IsEmailAddress(string emailAddress)
{
string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"

+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"

+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"

+ @"[a-zA-Z]{2,}))$";

Regex reStrict = new Regex(patternStrict);

bool isStrictMatch = reStrict.IsMatch(emailAddress);

return isStrictMatch;
}

Thanks,
Miguel
 
P

Peter Duniho

And if the string contains text with 2 emails in it ... but in the
text dots and @'s are also used but not in email addresses?

If you are not limiting the input of the method to strings which you
already know in advance are email addresses, then there are all sorts of
things that can go wrong.

IMHO, you should process the string in two passes: first, search for an
identify each element of the string that you feel meets whatever criteria
you have for "email address", and then second, just do a straight
search-and-replace.

You will still have either false positives or false negatives, depending
on your approach, but at least you won't be conflating the two pieces of
work into a single method.

Pete
 
G

Gregory A. Beamer

I need to obfuscate all emails in a string. e.g:

To replace an email "(e-mail address removed)" by "name(at)domain(dot).com".

I have been using the following but it only replaces @ and I am not
sure if this is the best way to do this:

public static String Obfuscate(String text, String at) {
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 fix = String.Concat("${start}", at, "${end}");
return text == null ? null : expression.Replace(text, @fix);
} // Obfuscate

I would like to be able to use replace the DOT to and maybe not using
a regex would be better?

This looks like an overly complex replace function. Find the email
address(es) in the string and replace it with one that has gone through
the "obfuscation" process. Peter's method would be fine, in a routine of
its own, to accomplish this. Just simply find the matches for address
and replace with an "obfuscated" version of the match.

If there are multiple email addresses, fix each as a single match rather
than trying to fix a huge pile of text with multiple addresses. If that
is what you are doing, that is.

The other way to approach this is not put the addresses into the text
until you have obfuscated. if you are protecting users from themselves,
this might not be an option for you.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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

Obfuscate Email 1
Replace email 1
Regex 3
Regex: Capturing HTML 1
Regular Expressions 4
Regex: replacing \n and spaces 4
Help with regular expression 2
Regex: Split string where line starts with known value? 2

Top