email address hiding some characters with c#, regex

regex mask email address c#
mask phone number c#
c# email validation
email masking code
email masking example
c# string mask characters
regex c#
c# hash email address

I would like replace some the chars of the e-mail addresses with * char.

when a customer make request, I would like to hide some the chars of the e-mail addres like below;

I would like to do that like that. I would like to show first two chars before @ and last 3 chars after @

but is there any other common way of doing this?

Similar to other responses, but also different. Accepts the addresses too.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Test
        public static void Main()
                String regex = @"(.{2}).+@.+(.{2}(?:\..{2,3}){1,2})";
                String replace = "$1*@*$2";
                List<String> tests = new List<String>(new String[]{
                tests.ForEach(email =>
                        Console.WriteLine(Regex.Replace(email, regex, replace));

Results in:


Though I'm not 100% sure what you want to do with names that only have 2 letters on either side (thus the last two results). But that's my bid. Example

how to do masking/hiding email address in c#, Note that masking the email always with a fixed number of characters will make it that you could achieve something very similar to what you are doing using Regex. I wrote this method as it was easier to customize for my specific masking  email address hiding some characters with c#, regex. Ask Question Although you'll need to first validate that the email address contains enough characters before

Because your rules are quite simple it might be easier to just use substring to get the characters before and after the @ and then replace them.

Something along the lines of

            int index = email.IndexOf('@');                
            string returnValue = email.Replace(email.Substring(index - 3, 3), "***").Replace(email.Substring(index+1,3), "***");

Although you'll need to first validate that the email address contains enough characters before the @ and change accordingly.

[Solved] Email Mask for c# masktextbox, Hi , I recommend for you using Regular Expression Check this. Hide Copy Code. private void BtnSave_Click(object sender, EventArgs e)  I have a directory with lots of folders, sub-folder and all with files in them. The idea of my project is to recurse through the entire directory, gather up all the names of the files and replace

You could do this:

resultString = Regex.Replace(subjectString, "([^@]{2})[^@]*@[^.]*([^.]{3}.*)$", "$1~~~@~~~$2");

This will fail, though, if there are less than three characters after the @ (like in, or less than 2 before the @. What would you want to happen in a case like that?

C# Mask email address for GDPR reasons, This is a C# extension method that will mask your email address following this If the first part of the email is shorter than 4 characters, the entire email will be return Regex. With your approach, you'd get fake collusion between unrelated users messing up statistics, and some addresses will still be  Is there a way to extract all email addresses from a plain text using C# . For example . my email address is and his email is should return ., I have tried the following but it matches perfect emails only.

public static string MaskEmailID(string EmailID)
        MailAddress addr = new MailAddress(EmailID);
        string username = addr.User;
        string domain = addr.Host;
        String regex;
        if (domain.Contains(".com"))
            regex = @"(.{1}).+(.{1})+@(.{1}).+(.{1}(?:\..{2,3}){1,2})";
            regex = @"(.{1}).+(.{1})+@(.{1}).+(.{4}(?:\..{2,3}){1,2})";
        string CharStr1 = new String('*', username.Length - 2);
        string CharStr2 = new String('*', (domain.IndexOf('.') - 2));
        String replace = "$1" + CharStr1 + "$2@$3" + CharStr2 + "$4";
        return Regex.Replace(EmailID, regex, replace);
    }, email masking regex c# i have an issue, i have to apply masking/hiding part of email address in c#. i have this code but its giving exception for some emails. If you always want to mask anything between first character and last character  Updated answer for 2019. Regex object is thread-safe for Matching functions. Knowing that and there are some performance options or cultural / language issues, I propose this simple solution.

how to do masking/hiding email address in c#, This is a C# extension that will mask email address or a simple string to Hide email address with * for GDPR reasons, for example. (first and last character for username and first 3 of domain plus suffix ".com", Because we respect your right to privacy, you can choose not to allow some types of cookies. It's not so interesting to include an exhausive list of top-level domains in the regex for example. Simply accepting any top-level domain will suffice. I'm not sure if address tags (e.g. are part of the RFCs I mentioned, but I would like the regex to validate these. IPv6 should definitly be handled correctly .

RegExMaskData - Overview, Each MaskType has different set of mask characters that are combined to form a xaml; c#. <syncmaskededit:SfMaskedEdit x:Name="maskedEdit" The regular expressions provide significant advantages when creating masks Example email, password, and more. Restricts you to enter specific range at specific position. You can use this behaviour in FluentValidation by calling RuleFor(x => x.Email).EmailAddress(EmailValidationMode.Net4xRegex). Note that this approach is deprecated and will generate a warning as regex-based email validation is not recommended.

MaskType in Xamarin Masked Entry control, A regular expression is a sequence of characters that define a search pattern. Usually such Some classes of regular languages can only be described by deterministic finite automata whose size grows exponentially in Hidden categories:. Regex, or Regular Expressions, is a sequence of characters, used to search and locate specific sequences of characters that match a pattern. In SQL if you were looking for email addresses from the same company Regex lets you define a pattern using comparators and Metacharacters , in this case using ~* and % to help define the pattern: