Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create Regex pattern for Email Addresses

For creating email addresses is allowed to insert only these characters, please help me to create regex that allows only these characters.

http://www.novell.com/documentation/groupwise2014r2/gwsdk_admin_rest_api/data/b12nem8w.html

Invalid Characters in Internet Email Addresses
Internet email addresses must include only RFC-compliant characters, which include:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Numbers 0-9
  2. Uppercase letters A-Z
  3. Lowercase letters a-z
  4. Plus sign +
  5. Hyphen –
  6. Underscore _
  7. Tilde ~

This is my regex, I don’t understand why this {} characters not remove

var input = "?}{)(&^%#@*/.09Aa+7-8_8~_8*!";

            Regex r = new Regex("(?:[^a-z0-9-+'-'-_-~])", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);                
            var dd =  r.Replace(input, "_");
            
            Console.WriteLine(dd);
            Console.Read();

enter image description here

>Solution :

Look at your pattern:

a-z0-9-+'-'-_-~

Breaking that down, you allow:

  • a to z
  • 0 to 9
  • -, +
  • ' to ' – a single character
  • _ to ~ – a range of 32 characters, which includes both { and }

Fix your regex pattern to only include the valid characters, and your code will work:

Regex r = new Regex("(?:[^a-z0-9\\-+'_~])", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);

Output:

_____________09_a+7-8_8~_8__

.NET Regular Expressions | Microsoft Docs

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading