4

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

FlipTop
  • 161
  • 1
  • 2
  • 8
  • 2
    Try to find a title that describes your issue better. Consider that this title will be used on search-engines. – Tim Schmelter Jan 27 '14 at 16:30
  • 1
    So close: Try \d instead of /d – Scott Wegner Jan 27 '14 at 16:30
  • 1
    backslash escapes funny characters like \d. The forward slash is written on either side of a Regex in many languages, denoting that the expression is a Regex. – La-comadreja Jan 27 '14 at 16:34
  • @TimSchmelter yes you are right, sorry I will change it, not sure if it will help anyone else though. New to this but hopefully will improve the quality of my contributions with time. Thanks everyone. – FlipTop Jan 27 '14 at 16:38

2 Answers2

8

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.

Jon
  • 3,065
  • 1
  • 19
  • 29
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
-1

Or don't use \d at all (preferred and faster):

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

See here or here for why.

Community
  • 1
  • 1
tenub
  • 3,386
  • 1
  • 16
  • 25