Forum Moderators: open
I will be testing against a single string, here are the rules:
1. string can be either 10 or 11 characters long (10 mandatory, 1 optional)
2. The first 4 characters must be [a-z] or [A-Z]
3. The next 6 characters (5-10) must be numeric [0-9]
4. The final character (which is optional) can be either 0-9 or the letter 'k' ([0-9][K][k]?)
Here is what I have so far, but it's incomplete:
string c = "ADXU220564";
if(Regex.IsMatch(c.ToLower(), @"[a-z]{4}\d{6}\w{1}"))
I appreciate the help ... Regular Expressions is something I could certainly benefit from learning more about, but unfortunatley I never get much time to spend learning
Thank you!
-Mike
This one works ...
\b[a-z,A-Z]{4}\d{6}[0-9,K,k]{0,1}\b
Don't use commas as separators inside a character class (the stuff in square brackets). As it stands you're allowing a comma in the first four digits and in the 11th slot. So something like ADX,220564, would give a false positive match.
Write it as emsaw suggests with the stuff in square brackets all crammed together.
Also starting with ^ and ending with $ means the string being tested must start right before the thing you're looking for and end right after it. So ^[a-zA-Z]{4}\d{6}[0-9Kk]{0,1}$ wouldn't match a string like I got a great deal on a ADXA220564k. For that you'd need to use the word boundary meta characters you had previously, so \b[a-zA-Z]{4}\d{6}[0-9Kk]{0,1}\b
mattglet, thank you for the suggestion, even though I didnt' need to use it I do appreciate the effort
-Mike
Those guys are usually quite sharp.
You won't find us guys over here [webmasterworld.com] taking a nap either.