tags:

views:

58

answers:

3

In c#, i gotta verify user inputs. Can someone tell me what will be the reg ex in order to verify this expression 12345-4521.

<12345> = Any five digits only
<-> = only hyphen
<4521> = Any four digits only but last digit should either be 0 or 1.
+4  A: 

This should do it:

\d{5}-\d{3}[01]

Explanation:

  • The \d matches any digit from 0 through 9
  • The [01] matches a 0 or 1
  • The {5} and {3} tells it to match exactly that number of the previous expression

This is pretty basic stuff. When you get a chance, you should read a good tutorial, which covers this (and much more).

Chris B.
Your solution is missing caret. donc not working as per need. Accepting more than 5 chars
Novice
No, it's not anchored; if you're not testing the length of the string you need to add `^` and `$`. Many regex libraries allow you to distinguish between a _match_ and a _search_, apparently c# does not, which requires you to be explicit.
Chris B.
+6  A: 
^\d{5}-\d{3}[01]$
Christian Hayter
`[01]` is more concise than `(0|1)`.
Amber
I stand corrected :)
Christian Hayter
A: 

The Following Should work: \d{5}\-\d{3}[01]