excel - 字符串验证

时间:2012-10-22 08:57:17

标签: excel parsing excel-formula

我需要验证字符串,例如

'AB01MCD10werdfGhjklOP01DEF03124'

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

结束
  1. 字符串以'AB'开头,必填
  2. 再次强制执行'CD'
  3. 跟随'OP'是可选的
  4. 紧随'EF',强制性
  5. 我想验证A1中的字符串并将验证结果放在B1中。

    如果验证成功,B1应该验证成功,否则错误:预期'CD',但找到'blah

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

4 个答案:

答案 0 :(得分:4)

RegExp验证字符串:

  1. AB 开头,然后是一个2位数字,用于设置要遵循的字母字符数
  2. 后跟 CD ,然后是一个2位数字,用于设置要遵循的字母字符的数量
  3. 可选 OP ,如果它还有,则还有2位数字,用于设置要遵循的字母字符的数量
  4. 后跟 EF ,然后是一个2位数字,用于设置要跟随的数字字符的数量
  5. 如果这是您想要的字符串,那么我将调整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
    

答案 1 :(得分: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

答案 2 :(得分: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位数,指示以下字符的长度

如果字符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)) & "}))$"

答案 3 :(得分:0)

根据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