1

我有需要验证的字符串,例如

'AB01MCD10werdfGhjklOP01DEF03124'

它以 开头AB,后跟一个两个字符的数字,它告诉我们 'AB' 后面有多少个字符(在这种情况下是01,只有 1 个字符是 M)。后面应该是CD, 再次具有相同的行为,然后是OP,这是可选的,即该OP组可能存在也可能不存在。最后它应该以EFgr结尾

  1. 字符串以 'AB' 开头,必填
  2. 其次是'CD'再次强制
  3. 后跟“OP”,这是可选的
  4. 后跟“EF”,必填

我想验证 A1 中的字符串并将验证结果放在 B1 中。

如果验证成功,B1 应该有Validation Success,否则Error:Expected 'CD', but found 'blah

请让我知道最好的方法 - 是使用公式还是宏?

4

4 回答 4

4

RegExp 验证了一个字符串:

  1. AB开头,然后是一个 2 位数字,用于设置要跟随的字母字符数
  2. 后跟CD,然后是一个 2 位数字,用于设置要跟随的字母字符数
  3. 可选的OP,如果存在,它也有 2 位数字,用于设置要跟随的字母字符的数量
  4. 后跟EF,然后是一个 2 位数字,用于设置要跟随的数字字符的数量

如果这是您想要的字符串,那么我将调整该Len部分以标记测试中断的位置(即错误:预期的“CD”)。尽管我注意到您可能会遇到最多 4 个潜在问题,而不是示例中的单个问题)。

如果您对任何字符(来自 Chris 的代码将接受)而不是您提供的字母和数字示例感到满意,那么RegExp可以简化。

Sub Test()
    Debug.Print Validator("AB01MCD10werdfGhjklOP01DEF03124") ' TRUE with optionalOP
    Debug.Print Validator("AB01MCD10werdfGhjklEF03124")      ' TRUE without optional OP
    Debug.Print Validator("AB01MCD10weardfGhjklOP01DEF03124")  ' fail as CD string is too long
End Sub

Function Validator(strIn As String) As Boolean
    Dim objRegex As Object
    Dim objRegexM As Object
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Pattern = "AB(\d{2})([a-z]+)CD(\d{2})([a-z]+)((?=OP)OP(\d{2})([a-z]+)){0,1}EF(\d{2})(\d+)"
        .ignoreCase = True
        If .Test(strIn) Then
            Set objRegexM = .Execute(strIn)
            With objRegexM(0)
                Validator = Len(.submatches(1)) = CLng(.submatches(0)) And Len(.submatches(3)) = CLng(.submatches(2)) And Len(.submatches(6)) = CLng(.submatches(5)) And Len(.submatches(8)) = CLng(.submatches(7))
            End With
        End If
    End With
End Function
于 2012-10-22T13:07:17.563 回答
1

基于EF术语也是形式EFnn<n characters>,试试这个

Function CheckString(r As Range) As String
    Dim str As String
    Dim substr As String
    Dim v As Long

    str = r.Value

    If Left$(str, 2) = "AB" Then
        str = Mid$(str, 3)
        substr = Left$(str, 2)
        str = Mid$(str, 3)
        If IsNumeric(substr) Then
            v = Val(substr)
            str = Mid$(str, v + 1)
            ' CD
            If Left$(str, 2) = "CD" Then
                str = Mid$(str, 3)
                substr = Left$(str, 2)
                str = Mid$(str, 3)
                If IsNumeric(substr) Then
                    v = Val(substr)
                    str = Mid$(str, v + 1)

                    ' OP or EF
                    If Left$(str, 2) = "OP" Then
                        str = Mid$(str, 3)
                        substr = Left$(str, 2)
                        str = Mid$(str, 3)
                        If IsNumeric(substr) Then
                            v = Val(substr)
                            str = Mid$(str, v + 1)

                            ' EF
                            If Left$(str, 2) = "EF" Then
                                str = Mid$(str, 3)
                                substr = Left$(str, 2)
                                str = Mid$(str, 3)
                                If IsNumeric(substr) Then
                                    v = Val(substr)
                                    If Len(str) = v Then
                                        CheckString = "Validation Success"
                                    Else
                                        ' No more after EF
                                        CheckString = "Error: Expecting " & v & " characters after EF"
                                    End If
                                Else
                                    ' number follows EF
                                    CheckString = "Error: Expected number following EF"
                                End If
                            Else
                                CheckString = "Error: Expecting EF"
                            End If
                        Else
                            ' number follows CD
                            CheckString = "Error: Expected number following CD"
                        End If

                    ' EF
                    ElseIf Left$(str, 2) = "EF" Then
                        str = Mid$(str, 3)
                        substr = Left$(str, 2)
                        str = Mid$(str, 3)
                        If IsNumeric(substr) Then
                            v = Val(substr)
                            If Len(str) = v Then
                                CheckString = "Validation Success"
                            Else
                                ' No more after EF
                                CheckString = "Error: Expecting " & v & " characters after EF"
                            End If
                        Else
                            ' number follows EF
                            CheckString = "Error: Expected number following EF"
                        End If
                    Else
                        CheckString = "Error: Expecting EF"
                    End If

                Else
                    ' number follows CD
                    CheckString = "Error: Expected number following CD"
                End If
            Else
                ' Begin with "CD"
                CheckString = "Error: Expected CD"
            End If
        Else
            ' number follows AB
            CheckString = "Error: Expected number following AB"
        End If
    Else
        ' Begin with "AB"
        CheckString = "Error: Expected AB"
    End If
End Function
于 2012-10-22T11:31:55.067 回答
0

这是第一次破解,可能有更好的方法,正则表达式不是我的事,但它适用于示例:

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

根据 Chris 的建议,经过编辑以强制 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)) & "}))$"
于 2012-10-22T10:37:43.720 回答
0

我写了这个函数来为你完成这项工作。但是如果你有超过 AB/CD/EF 那么你将不得不相应地修改代码

Private Sub Command1_Click()
Dim A1 As String
Dim B1 As String

A1 = "AB01MCD10werdfGhjklOP01DEF03124"
B1 = CodeValidate(A1)

MsgBox B1

End Sub



Function CodeValidate(ByVal TXT As String) As String
    Dim AB As Boolean
    Dim CD As Boolean
    Dim EF As Boolean
    Dim OP As Boolean

    Dim NextCounter As Integer
    Dim Buffer As String

    Dim result As String


    For i = 1 To Len(TXT)
        Buffer = Buffer & Mid(TXT, i, 1)
        If Len(Buffer) = 2 Then
        '============
            Select Case Buffer
            Case "AB"
                If AB = False And CD = False And EF = False Then
                    AB = True
                    NextCounter = Val(Mid(TXT, i + 1, 2))
                    i = i + 2 + NextCounter
                    Buffer = ""
                Else
                    result = "Error: Duplicate [AB] found at position " & i & "."
                    Exit For
                End If
            Case "CD"
                If AB = True And CD = False And EF = False Then
                    CD = True
                    NextCounter = Val(Mid(TXT, i + 1, 2))
                    i = i + 2 + NextCounter
                    Buffer = ""
                Else
                    result = "Error: Duplicate [CD] found at position " & i & "."
                    Exit For
                End If
            Case "EF"
                If AB = True And CD = True And EF = False Then
                    EF = True
                    NextCounter = Val(Mid(TXT, i + 1, 2))
                    i = i + 2 + NextCounter
                    Buffer = ""
                Else
                    result = "Error: Duplicate [EF] found at position " & i & "."
                    Exit For
                End If
            Case "OP"
                If OP = False Then
                    OP = True
                    NextCounter = Val(Mid(TXT, i + 1, 2))
                    i = i + 2 + NextCounter
                    Buffer = ""
                Else
                    result = "Error: Duplicate [OP] found at position " & i & "."
                    Exit For
                End If
            Case Else
                If AB = False Then
                    result = "Error: Expecting [AB] but found [" & Buffer & "] at position " & i & "."
                    Exit For
                ElseIf AB = True And CD = False Then
                    result = "Error: Expecting [CD] but found [" & Buffer & "] at position " & i & "."
                    Exit For
                ElseIf AB = True And CD = True And EF = False Then
                    result = "Error: Expecting [EF] but found [" & Buffer & "] at position " & i & "."
                    Exit For
                End If
            End Select
        '============
        End If
    Next

    If result = "" Then
        result = "Validation Success"
    End If

    CodeValidate = result

End Function
于 2012-10-22T10:37:34.070 回答