Results 1 to 3 of 3

Thread: Testing number of digits with Regular Expressions

  1. #1
    VBobCat's Avatar
    VBobCat is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    117
    Reputation
    25

    Question Testing number of digits with Regular Expressions

    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!

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,202
    Reputation
    2369
    Use the | (or) operator: (\d{2}|\d{4})

  3. #3
    VBobCat's Avatar
    VBobCat is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2011
    Location
    São Paulo, Brazil
    Posts
    117
    Reputation
    25

    Thumbs up

    Quote Originally Posted by JohnH View Post
    Use the | (or) operator: (\d{2}|\d{4})
    Thank you very much!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking