I want the string (length 8) format to be one letter from a specific letter list A, B, C (neither of these letters should be repeated) the letters have a random position and the rest are numbers (0-9). For example:
1234A567 - Valid
277897C0 - Valid
A100299B - Not valid
12C3879C - Not valid
I tried something like this:
(\d){7}(A|C|E|F|G|H|J|K|M|U|Z){1}
but this does not work.
Help?
The current regular expression you have will only match a string with 7 digits and one letter at the end - however you want the letter to be at any random place in the string - so you have to do something different.
Here's a regular expression that will validate everything you need except the total length of the string (but that can be simply tested using str.Length == 8
):
^\d{0,7}[A|C|E|F|G|H|J|K|M|U|Z]{1}\d{0,7}$
Here's a simple demo:
var re = new Regex(@"^\d{0,7}[A|C|E|F|G|H|J|K|M|U|Z]{1}\d{0,7}$");
var strings = new string[] {
"1234A567", // - Valid
"277897C0", // - Valid
"A100299B", // - Not valid
"12C3879C" // - Not valid
};
foreach(var str in strings)
{
Console.WriteLine(str + (str.Length == 8 && re.IsMatch(str) ? " - valid" : " - not valid"));
}
Results:
1234A567 - valid
277897C0 - valid
A100299B - not valid
12C3879C - not valid