分析字母数字字符串的格式

时间:2012-04-10 00:18:09

标签: regex vba

我正在尝试编写一个接受字符串的函数,解析它,并返回另一个字符串,该字符串汇总了原始字符串中连续的字母或数字字符的数量。

例如,字符串999aa45bbx将返回3N2A2N3A, 即。

  • 3个数字,
  • 后跟2 alpha,
  • 乘2个数字,
  • by 3 alpha。

我正在使用该功能来分析保险单ID号的格式。到目前为止,我发现在线解决方案提取字母或数字字符,但没有描述原始字符串中存在这些字符的格式或顺序。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:8)

像这样的正则表达式将完成这项工作

  • 同时按 进入VBE
  • 插入模块
  • 复制并粘贴以下代码
  • 同时按 返回Excel

然后您可以在Excel中使用该函数(也检测无效字符串),即在B1中 =AlphaNumeric(A1)

enter image description here

Function AlphaNumeric(strIn As String) As String
    Dim objRegex As Object
    Dim objRegMC As Object
    Dim objRegM As Object
    Dim strOut As String
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Global = True
        .ignorecase = True
        .Pattern = "[^\w]"
        If .test(strIn) Then
            AlphaNumeric = "One or more characters is invalid"
        Else
            .Pattern = "(\d+|[a-z]+)"
            Set objRegMC = .Execute(strIn)
            For Each objRegM In objRegMC
                strOut = strOut & (objRegM.Length & IIf(IsNumeric(objRegM), "N", "A"))
            Next
            AlphaNumeric = strOut
        End If
    End With
End Function

答案 1 :(得分:4)

老派,循环遍历字符串中的所有字符:

Function IdentifyCharacterSequences(s As String) As String
    Dim i As Long
    Dim charCounter As Long
    Dim currentCharType As String
    Dim sOut As String

    sOut = ""
    charCounter = 1
    currentCharType = CharType(Mid(s, 1, 1))

    For i = 2 To Len(s)
        If (Not CharType(Mid(s, i, 1)) = currentCharType) Or (i = Len(s)) Then
            sOut = sOut & charCounter & currentCharType
            currentCharType = CharType(Mid(s, i, 1))
            charCounter = 1
        Else
            charCounter = charCounter + 1
        End If
    Next i

    IdentifyCharacterSequences = sOut
End Function

这使用以下辅助函数。请注意,使用字母“X”标识非字母数字字符。您可以轻松修改它以满足您的目的。

Function CharType(s As String) As String
    If s Like "[A-z]" Then
        CharType = "A"
    ElseIf s Like "[0-9]" Then
        CharType = "N"
    Else
        CharType = "X"
        'Or raise an error if non-alphanumerical chars are unacceptable.
    End If
End Function

用法示例:

enter image description here