skip to Main Content

I need a regex for the following pattern:

  • Total of 4 characters (numeric, alpha, numeric, numeric).
  • first character must be a number(0-9)
  • second character must be a number(A-M i should not come) -> this refer to month jan to dec
    -third and fourth characters must be a number(0-9) -> Date

Examples:

  • 3K01 is valid
  • 3K40 is invalid
  • 5I01 is invalid -> A-M
  • 1A01 is valid
Dim validateLot As String = "/^((0?[1-9]|1[012])M|(0?[1-9]|[12][0-9]|3[01])D)$/"
 
Dim r As New Regex(validateLot)
    
If (r.IsMatch(txtCarton0.Text)) Then
Else
End If

2

Answers


  1. Is this what you need

    @"(?=.{4})((?=.{2}3[01])|(?=.{2}[0-2]d))^[0-9][^IN-Zd][0-3][0-9]$"gm
    

    https://regex101.com/r/4Y9fcU/1

    Login or Signup to reply.
  2. So, there are 4 characters, numeric, alpha, numeric, numeric.
    They represent as follows:

    • first character must be a number(0-9) ≡ regex: d or [0-9]
    • second character must be a letter between A-M, without I ≡ regex: [A-HJ-M]
    • third & fourth characters are represents Calendar Date (01-31)
      Date: 
          01-09: ([0][1-9])
          10-29: ([1-2][0-9])
          30-31: ([3][0-1])
    

    So, third & fourth character matching regex: (([0][1-9])|([1-2][0-9])|([3][0-1]))

    enter image description here

    Therefore your regex should be:

    ^[0-9][A-HJ-M](([0][1-9])|([1-2][0-9])|([3][0-1]))$
    or
    ^d[A-HJ-M](([0][1-9])|([1-2]d)|([3][0-1]))$
    

    Test regex: https://regex101.com/r/7AImf2/1

    Thank You

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search