0

I need regex for the following text validation:

must start with two alpabetic characters, six digits, minus and two digits represent the year, for example: Ew142356-19

I have tried:

 Console.WriteLine(new Regex("[0-9]{2}[a-zA-Z]{6}[\\-]{1}[1-9]{1}[0-9]{1}").IsMatch("Be123456-19"));

but it prins False

BugsFixer
  • 377
  • 2
  • 15

1 Answers1

2

Try this one:

^[a-zA-Z]{2}\d{6}-[1-9]\d$

Notes:

There is no need to do something like [1-9]{1}. [1-9] already means that this is one element.

Use Anchors: ^ means the start of the string and $ means the end of it. If you dont use them you could match something aBe123456-19

\d is equivalent to [0-9]

There is no need to create a set for the - sign

Superluminal
  • 947
  • 10
  • 23