my client wants to mask the emails in a message, following this way:
Original email:
2 [email protected] --- > can be anything like gov.co, .com.mx.. etc
masked email:
1 u*****e@d****n.com
2 u*****o@d****n.com.co
For First case, I have this
string pattern = @"(?<=[\w]{1})[\w-\._\+%]*(?=[\w]{1}@)"; // ---> mask before "@"
string p2 = @"(?<=[\w]{1})[\w-\+%]*(?=[\w]{1}[.])"; // --- > mask after "@"
string result = Regex.Replace(mail, pattern, m => new string('*', m.Length));
string newresult = Regex.Replace(result, p2, m => new string('*', m.Length));
Console.WriteLine("Masked email: {0}", newresult);
and works fine:
but... doesn't work for the second case...
so, what is the regular expression that applies to both cases for mask after "@" ?
[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times.
To mask an email, all names must be converted to lowercase and all letters between the first and last letter of the first name must be replaced by 5 asterisks '*'. 2. Phone number: A phone number is a string consisting of only the digits 0-9 or the characters from the set {'+', '-', '(', ')', ' '}.
You should not use regular expressions to validate email addresses.
See the edit at the bottom of my answer for the second method by which this can be accomplished in .net (much shorter).
See regex in use here
(?:(?:^|(?<=@))([^.@])|\G(?!\A))[^.@](?:([^.@])(?=[.@]))?
Replacement: $1*$2
See code in use here
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?:(?:^|(?<=@))([^.@])|\G(?!\A))[^.@](?:([^.@])(?=[.@]))?";
string substitution = @"$1*$2";
string input = @"[email protected]
[email protected]";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
Console.WriteLine(regex.Replace(input, substitution));
}
}
[email protected]
[email protected]
u*****e@d****n.com
u*****o@d****n.com.co
(?:(?:^|(?<=@))([^.@])|\G(?!\A))
Match either of the following
(?:^|(?<=@))([^.@])
Match the following
(?:^|(?<=@))
Match either of the following
^
Assert position at the start of the line(?<=@)
Positive lookbehind ensuring what precedes is the at sign character @
literally([^.@])
Capture any character not present in the list (any character except the dot .
or at sign @
characters literally) into capture group 1\G(?!\A)
Assert position at the end of the previous match[^.@]
Match any character not present in the list (any character except the dot .
or at sign @
characters literally)(?:([^.@])(?=[.@]))?
Match the following zero or once
([^.@])
Capture any character not present in the list (any character except the dot .
or at sign @
characters literally) into capture group 2(?=[.@])
Positive lookahead ensuring what follows is a dot .
or at sign @
character literallyThis pattern obtains the same results as my original answer (unless a string of length 2 is given: i.e. [email protected]
is left alone while the original answer will make this u*@domain.com
).
C# (.net) supports variable length lookbehinds. Credit to @Gurman with his comment. He was on the right track, just probably wasn't aware that .net supports variable length lookbehinds.
See regex in use here
(?<=(?:^|@)[^.]*)\B.\B
(?<=(?:^|@)[^.]*)
Positive lookbehind ensuring what follows matches
(?:^|@)
Match either a start of the line assertion or a literal at sign @
[^.]*
Match any character except the dot character .
literally\B
Match a position where a word boundary doesn't match.
Match any character\B
Match a position where a word boundary doesn't matchRegex for emails containing .
in localpart (see in use here):
(?<=^[^@]+)[^@](?=[^@])|(?<=@[^.]+)[^.](?=[^.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With