I need to find a pattern that restricts the input of data. What I need it to restrict input to:
First character must be "S" Second character must be "S", "T", "W", "X" or "V" The next 6 must be numbers for 0 - 9 The last 2 can be any capital letter or any number 0 - 9
So my research led me to put this together.....
^[S][STWXV]/d{6}[A-Z0-9]{2}$
From what I read:
[S]
mean the capital letter S only
[STWXV]
means any one letter from this list
/d{6}
means 6 digits
[A-Z0-9]{2}
means any 2 characters A - Z or 0 - 9
I am not looking for a match anywhere in a string, i need the whole string to match this pattern.
So why does Regex.IsMatch("SX25536101", "^[S][STWXV]/d{6}[A-Z0-9]{2}$")
return false?
Ive obviously gone wrong somewhere but this is my first attempt at Regular Expressions and this makes no sense :(
You need to use \d
for digits:
Regex.IsMatch("SX25536101", @"^[S][STWXV]\d{6}[A-Z0-9]{2}$"); // true
NOTE: Here is nice Regular Expressions Cheat Sheet which can help you in future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With