Answered by:
c# equivalent code for the script

Question
-
Hello All,
I need below code equivalent in c#. Please help me
import java.util.regex.* // regex' String SPCHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]" String VARS = "(\\\\.)|[^\\s${SPECIAL_CHARS}]"; String QUOTSER = "(\"(\\\\\"|[^\"])*\")" String WORD = "((${VALID_CHARS}|')+|${QUOTED_USER})" String EMREGEX = "^\\s*?(.+)@(.+?)\\s*\$" String USREGEX = "\\s*${WORD}(\\.${WORD})*"
Thanks in Advance
Chandu
- Edited by How to remove xml namespace prefixes Thursday, January 10, 2019 2:06 PM
Wednesday, January 9, 2019 11:08 AM
Answers
-
Hi ,
Thank you for posting here.
According to your description, you want to convert java code to c#.
Please refer to the following code.
static bool checkemail(string email) { String EMAIL_REGEX = @"[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+"; String USER_REGEX = @"[A-Za-z0-9\u4e00-\u9fa5]+"; int? MAX_USERNAME_LEN = 64; try { bool ismail = Regex.IsMatch(email, EMAIL_REGEX); if (email.IndexOf('@')==-1) { return false; } else { string user = email.Substring(0, email.LastIndexOf('@')); bool isuservalid = Regex.IsMatch(email, USER_REGEX); bool lengthcheck; if(user.Length>MAX_USERNAME_LEN) { lengthcheck = false; } else { lengthcheck = true; } return ismail & isuservalid & lengthcheck; } } catch(Exception e) { Console.WriteLine(e.ToString()); return false; } }
Hope my suggestion could be helpful.
Best regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Jack J JunMicrosoft contingent staff Thursday, January 10, 2019 6:01 AM
- Marked as answer by How to remove xml namespace prefixes Friday, January 18, 2019 5:56 AM
Thursday, January 10, 2019 5:19 AM -
Evaluate this approach too:
static bool checkEmails( string email ) { try { new MailAddress( email ); return true; } catch( FormatException ) { return false; } }
- Proposed as answer by Zhanglong WuMicrosoft contingent staff Wednesday, January 16, 2019 1:10 AM
- Marked as answer by How to remove xml namespace prefixes Friday, January 18, 2019 5:56 AM
Thursday, January 10, 2019 12:48 PM
All replies
-
You can use the MSDN function
IsValidEmail
from How to: Verify that Strings Are in Valid Email FormatWednesday, January 9, 2019 11:12 AM -
Hi ,
Thank you for posting here.
According to your description, you want to convert java code to c#.
Please refer to the following code.
static bool checkemail(string email) { String EMAIL_REGEX = @"[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+"; String USER_REGEX = @"[A-Za-z0-9\u4e00-\u9fa5]+"; int? MAX_USERNAME_LEN = 64; try { bool ismail = Regex.IsMatch(email, EMAIL_REGEX); if (email.IndexOf('@')==-1) { return false; } else { string user = email.Substring(0, email.LastIndexOf('@')); bool isuservalid = Regex.IsMatch(email, USER_REGEX); bool lengthcheck; if(user.Length>MAX_USERNAME_LEN) { lengthcheck = false; } else { lengthcheck = true; } return ismail & isuservalid & lengthcheck; } } catch(Exception e) { Console.WriteLine(e.ToString()); return false; } }
Hope my suggestion could be helpful.
Best regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Jack J JunMicrosoft contingent staff Thursday, January 10, 2019 6:01 AM
- Marked as answer by How to remove xml namespace prefixes Friday, January 18, 2019 5:56 AM
Thursday, January 10, 2019 5:19 AM -
The following code library provides method to validate email addresses and goes into details how some approach are good vs bad, read the various "Strike" paragraphs as there are patterns that you may not have considered.
In the unit test for the library the following are used to test against.
namespace EmailAddressLibrary.Tests.CodeFool { using System.Collections.Generic; /// <summary> /// Examples borrowed from the CodeFool blog. /// http://codefool.tumblr.com/post/15288874550/list-of-valid-and-invalid-email-addresses /// </summary> internal static class Examples { internal static IEnumerable<string> Valid = new[] { @"1234567890@example.com", @"_______@example.com", @"email@[123.123.123.123]", @"email@example-one.com", @"email@example.co.jp", @"email@example.com", @"email@example.museum", @"email@example.name", @"email@subdomain.example.com", @"firstname+lastname@example.com", @"firstname-lastname@example.com", @"firstname.lastname@example.com", @"""email""@example.com", @"あいうえお@example.com", }; internal static IEnumerable<string> Invalid = new[] { @"#@%^%#$@#$@#.com", @".email@example.com", @"@example.com", @"Abc..123@example.com", @"Joe Smith <email@example.com>", @"email..email@example.com", @"email.@example.com", @"email.example.com", @"email@-example.com", @"email@111.222.333.44444", @"email@example", @"email@example..com", @"email@example.com (Joe Smith)", @"email@example@example.com", @"just""not""right@example.com", @"plainaddress", @"this\ is""really""not\allowed@example.com", @"""(),:;<>[]@example.com", @"email@123.123.123.123", }; } }
Basic usage
using EmailAddressLibrary; EmailAddressValidator.Msdn("foo@bar.com"); // True EmailAddressValidator.ReferenceSource("foo@bar.com"); // True EmailAddressValidator.Haacked("foo@bar.com"); // True EmailAddressValidator.JStedfast("foo@bar.com"); // True
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
- Proposed as answer by Zhanglong WuMicrosoft contingent staff Wednesday, January 16, 2019 1:10 AM
Thursday, January 10, 2019 12:07 PM -
Evaluate this approach too:
static bool checkEmails( string email ) { try { new MailAddress( email ); return true; } catch( FormatException ) { return false; } }
- Proposed as answer by Zhanglong WuMicrosoft contingent staff Wednesday, January 16, 2019 1:10 AM
- Marked as answer by How to remove xml namespace prefixes Friday, January 18, 2019 5:56 AM
Thursday, January 10, 2019 12:48 PM