【问题标题】:Retrieve alpha characters from alphanumeric string从字母数字字符串中检索字母字符
【发布时间】:2014-07-11 06:03:10
【问题描述】:

如何将AB2468123 分开

我尝试了以下方法:

myStr = "AB2468123"
split(myStr, "1" OR "2" OR "3"......."9")

我只想得到字母(字母)。

谢谢。

【问题讨论】:

  • 你想要什么结果? AB2468123?
  • 看起来很适合 Regexexample
  • @chrisneilsen - 根据您的建议,我添加了regex 答案!

标签: excel-vba excel vba


【解决方案1】:

如何仅从输入字符串中检索字母:

Function GetLettersOnly(str As String) As String
    Dim i As Long, letters As String, letter As String

    letters = vbNullString

    For i = 1 To Len(str)
        letter = VBA.Mid$(str, i, 1)

        If Asc(LCase(letter)) >= 97 And Asc(LCase(letter)) <= 122 Then
            letters = letters + letter
        End If
    Next
    GetLettersOnly = letters
End Function

Sub Test()
    Debug.Print GetLettersOnly("abc123")      // prints "abc"
    Debug.Print GetLettersOnly("ABC123")      // prints "ABC"
    Debug.Print GetLettersOnly("123")         // prints nothing
    Debug.Print GetLettersOnly("abc123def")   // prints "abcdef"
End Sub

编辑:为了完整起见(和 Chris Neilsen),这里是 Regex 方式:

Function GetLettersOnly(str As String) As String
    Dim result As String, objRegEx As Object, match As Object

    Set objRegEx = CreateObject("vbscript.regexp")

    objRegEx.Pattern = "[a-zA-Z]+"
    objRegEx.Global = True
    objRegEx.IgnoreCase = True

    If objRegEx.test(str) Then
        Set match = objRegEx.Execute(str)
        GetLettersOnly = match(0)
    End If
End Function

Sub test()
    Debug.Print GetLettersOnly("abc123") //prints "abc"
End Sub

【讨论】:

  • 如果相关注释将只返回第一个匹配项(使这一行冗余objRegEx.Global = True) - 将适用于abc123,但不适用于abc123d
【解决方案2】:

更简单的单拍RegExp

Sub TestIt()
MsgBox CleanStr("AB2468123")
End Sub

Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "[^a-zA-Z]+"
    .Global = True
    CleanStr = .Replace(strIn, vbNullString)
End With
End Function

【讨论】:

    【解决方案3】:

    这是我发现效果最好的。它可能有点基本,但它可以完成工作:)

        Function Split_String(Optional test As String = "ABC111111") As Variant
        For i = 1 To Len(test)
        letter = Mid(test, i, 1)
            If IsNumeric(letter) = True Then
               justletters = Left(test, i - 1)
               justnumbers = Right(test, Len(test) - (i - 1))
               Exit For
            End If
        Next
       'MsgBox (justnumbers)
       'MsgBox (justletters)
    
       'just comment away the answer you want to have :)
       'Split_String = justnumbers
       'Split_String = justletters
    
       End Function
    

    【讨论】:

    • 我认为你混淆了justnumbersjustletters
    • 当输入字符串是字母后跟数字时有效,例如abc123。但是它不适用于1abc123abc123abc。只要 OP 总是有字母后跟数字,就可以了。
    【解决方案4】:

    可能最快的方法是解析Byte String

    Function alpha(txt As String) As String
      Dim b, bytes() As Byte: bytes = txt
      For Each b In bytes
        If Chr(b) Like "[A-Za-z]" Then alpha = alpha & Chr(b)
      Next b
    End Function
    
    • 更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      相关资源
      最近更新 更多