Question Testing number of digits with Regular Expressions

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
I have to test a string that must match this pattern: any number of digits, a slash, and two or four digits, but not three (it is a year). So I'm using this boolean expression:

System.Text.RegularExpressions.Regex.IsMatch(texto, "^\d+/\d{2,4}$") And Not System.Text.RegularExpressions.Regex.IsMatch(texto, "^\d+/\d{3}$")


Here is my question: is there a way to test for two or four digits, but not three, in a single RegEx pattern?

Thank you very much!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Use the | (or) operator: (\d{2}|\d{4})
 

Latest posts

Top Bottom