Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern not matching input string

Tags:

c#

regex

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 :(

like image 348
FlipTop Avatar asked Mar 21 '23 19:03

FlipTop


1 Answers

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.

like image 73
Sergey Berezovskiy Avatar answered Apr 01 '23 22:04

Sergey Berezovskiy