Regular expression pattern not matching input line

I need to find a template that restricts data entry. What I need to limit input:

The first character must be "S" The 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 uppercase letter or any number 0 - 9

So, my research has led me to bring this together.

^[S][STWXV]/d{6}[A-Z0-9]{2}$

From what I read:

[S]means only the capital letter S [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'm not looking for a match anywhere in the string, I need the whole string to match this pattern.

So why Regex.IsMatch("SX25536101", "^[S][STWXV]/d{6}[A-Z0-9]{2}$")returns false?

Obviously, I was wrong, but this is my first attempt at regular expressions, and this does not make sense :(

+4
source share
2 answers

You need to use \dfor numbers:

 Regex.IsMatch("SX25536101", @"^[S][STWXV]\d{6}[A-Z0-9]{2}$"); // true

NOTE. The Regular Cheats Sheet is nice here , which may help you in the future.

+8
source

Or do not use \dat all (preferred and fast):

^S[STWXV][0-9]{6}[A-Z0-9]{2}$

See here or here for why.

-1
source

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


All Articles