Semicolon string confirmation

I have a string that should only be valid if it has 2 characters and is divided by commas.

AD,AC,AN,JP (valid) AD (valid) if user enter only one it must be validate 

Invalid must be returned if this number or any other character except a comma or its length is more than 2.

 AD,12,AN,JP (invalid) AAD,12,AN,JP (invalid) AA,CC,ANA,JP (invalid) AA,#C,AA,JP (invalid) 
+4
source share
1 answer

This assumes the input is always uppercase:

 var reggie = new Regex(@"^[AZ]{2}(,[AZ]{2})*$"); 

If, in addition to checking the input, you want to extract the data, you can perform a simple breakdown by comma (separation of regular expressions is not required):

 if (reggie.IsMatch(inputString)) string[] values = string.Split(','); 
+8
source

Source: https://habr.com/ru/post/1440763/


All Articles