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

4
  • 2
    Try to find a title that describes your issue better. Consider that this title will be used on search-engines. Commented Jan 27, 2014 at 16:30
  • 1
    So close: Try \d instead of /d Commented Jan 27, 2014 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. Commented Jan 27, 2014 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. Commented Jan 27, 2014 at 16:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How annoying to get so close an not spot such a silly error!! Thanks.
-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.

2 Comments

why is that preferred? -1
\d is shorthand for digits and it makes the Regex more concise. Use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.