Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask domain email address using regular expression

Tags:

c#

regex

email

my client wants to mask the emails in a message, following this way:

Original email:

1 [email protected]

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:

MaskedEmail first case

but... doesn't work for the second case...

so, what is the regular expression that applies to both cases for mask after "@" ?

like image 794
makitocode Avatar asked Dec 04 '17 15:12

makitocode


People also ask

What is the regex for email?

[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.

How do I mask an email address in Java?

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 {'+', '-', '(', ')', ' '}.

Can we use below given regular expression to validate an email address?

You should not use regular expressions to validate email addresses.


1 Answers

Original Answer

See the edit at the bottom of my answer for the second method by which this can be accomplished in .net (much shorter).

Code

See regex in use here

(?:(?:^|(?<=@))([^.@])|\G(?!\A))[^.@](?:([^.@])(?=[.@]))?

Replacement: $1*$2

Usage

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));
    }
}

Results

Input

[email protected]
[email protected]

Output

u*****e@d****n.com
u*****o@d****n.com.co

Explanation

  • (?:(?:^|(?<=@))([^.@])|\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 literally


Edit

This 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.

Code

See regex in use here

(?<=(?:^|@)[^.]*)\B.\B

Explanation

  • (?<=(?:^|@)[^.]*) 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 match

Edit 2

Regex for emails containing . in localpart (see in use here):

(?<=^[^@]+)[^@](?=[^@])|(?<=@[^.]+)[^.](?=[^.])
like image 55
ctwheels Avatar answered Oct 04 '22 22:10

ctwheels