これが最初の亀裂です。もっと良い方法があるかもしれません。正規表現は私のものではありませんが、例としては機能します。
Public Function ValString(strInput As String) As String
Static regEx As Object
Dim x As Long
Dim temp$
Dim matches As Object
If regEx Is Nothing Then Set regEx = CreateObject("vbscript.regexp")
With regEx
.Pattern = "^AB([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "AB") + 2, 2)) & "})CD([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "CD") + 2, 2)) & "})(OP([0-9]{2})[A-Za-z]+EF[0-9]+|EF[0-9]+)$"
Set matches = .Execute(strInput)
If .Test(strInput) Then
ValString = "Validation Success"
Exit Function
Else
.Pattern = "(AB|CD|EF)"
.Global = True
Set matches = .Execute(strInput)
If matches.Count > 0 Then
temp$ = "ABCDEF"
For x = 0 To matches.Count - 1
temp$ = Replace(temp$, matches(x), vbNullString)
ValString = ValString & ",'" & matches(x) & "'"
Next x
If Len(temp$) > 1 Then
ValString = "Error:Expected '" & temp$ & "', Found " & Right(ValString, Len(ValString) - 1)
Else
ValString = "Error:Paramaters Found, invalid string"
End If
Else
ValString = "Error:Expected 'CD', Found nothing"
End If
End If
End With
End Function
クリスの提案に従って、次の文字の長さを指示するABとCDに続く2桁を強制するように編集されました
Characters OPが同じパターンに従う場合は、次を使用できます。
.Pattern = "^AB([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "AB") + 2, 2)) & "})CD([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "CD") + 2, 2)) & "})(OP([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "OP") + 2, 2)) & "})EF[0-9]+|EF[0-9]+)$"
上記と同じですが、EFも同じパターンに従いますが、数字のみです。
.Pattern = "^AB([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "AB") + 2, 2)) & "})CD([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "CD") + 2, 2)) & "})(OP([0-9]{2})([A-Za-z]{" & Val(Mid(strInput, InStr(1, strInput, "OP") + 2, 2)) & "})(EF[0-9]{2})([0-9]{" & Val(Mid(strInput, InStr(1, strInput, "EF") + 2, 2)) & "})|(EF[0-9]{2})([0-9]{" & Val(Mid(strInput, InStr(1, strInput, "EF") + 2, 2)) & "}))$"