VB.NET Using regex instead of multiple IFs

VB.NET Using regex instead of multiple IFs

I am trying to find a way to use regex instead of the multiple IFs shown below.

  Private Function ValidateEMWINHeader(ByVal instream As Byte()) As Boolean

      Dim headerStr As String

      headerStr = System.Text.Encoding.ASCII.GetString(instream, 0, 86)

      If Strings.Mid(headerStr, 7, 3) <> "/PF" Then Return False

      If Strings.Mid(headerStr, 22, 3) <> "/PN" Then Return False

      If Strings.Mid(headerStr, 31, 3) <> "/PT" Then Return False

      If Strings.Mid(headerStr, 40, 3) <> "/CS" Then Return False

      If Strings.Mid(headerStr, 50, 3) <> "/FD" Then Return False

      If Strings.Mid(headerStr, 85, 2) <> (Chr(13) & Chr(10)) Then Return False

      Return True

  End Function

I would like to find a single line entry to perform this task.

Answer

I don't know if you can use RegEx in this case, because you need to call Strings.mid with different arguments. Try putting an Or between the conditions:

Private Function ValidateEMWINHeader(ByVal instream As Byte()) As Boolean

      Dim headerStr As String

      headerStr = System.Text.Encoding.ASCII.GetString(instream, 0, 86)

      If Strings.Mid(headerStr, 7, 3) <> "/PF" Or Strings.Mid(headerStr, 22, 3) <> "/PN" Or Strings.Mid(headerStr, 31, 3) <> "/PT" Or Strings.Mid(headerStr, 40, 3) <> "/CS" Or Strings.Mid(headerStr, 50, 3) <> "/FD" Or Strings.Mid(headerStr, 85, 2) <> (Chr(13) & Chr(10)) Then Return False

      Return True

  End Function

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles