1

検証する必要のある文字列があります。

'AB01MCD10werdfGhjklOP01DEF03124'

で始まりAB、その後に2文字の数字が続きます。これは、「AB」の後に続く文字数を示します(この場合は01、Mである1文字だけです)。その後にCD、同じ動作で、が続くOP必要があります。これはオプションです。つまり、OPグループが存在する場合と存在しない場合があります。最後にそれはEFgrで終わるはずです

  1. 文字列は「AB」で始まり、必須
  2. 続いて「CD」が再び必須
  3. オプションの「OP」が続きます
  4. 続いて「EF」、必須

A1で文字列を検証し、検証結果をB1に入れたいと思います。

検証が成功した場合、B1は検証成功を持っているはずです。そうでない場合、Error:Expected'CD'ですが、'blahが見つかりました

これを行うための最良の方法を教えてください-それは数式またはマクロを使用していますか?

4

4 に答える 4

4

これによりRegExp 、次の文字列が検証されます。

  1. ABで始まり、その後に続くアルファベット文字の数を設定する2桁の数字
  2. CDが続き、その後に続くアルファベット文字の数を設定する2桁の数字
  3. オプションのOP 。存在する場合は、続くアルファベット文字の数を設定する2桁の数字もあります。
  4. EFが続き、その後に続く数字の数を設定する2桁の数字

これが文字列に必要なものである場合は、Lenセクションを微調整して、テストが中断した場所にフラグを立てます(つまり、Error:Expected'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

私はあなたのために仕事をするこの関数を書きました。ただし、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 に答える
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

クリスの提案に従って、次の文字の長さを指示する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 に答える