Tokenizing Strings

我的未来我决定 提交于 2019-12-10 09:54:10

问题


I have around 100 rows of text that I want to tokenize, which are alike the following:

<word> <unknown number of spaces and tabs> <number>

I am having trouble finding tokenize functions with VBA. What would be the easiest method to tokenize such strings in VBA?


回答1:


You could read line by line and use the split function to split the word and number by space. I vaguely remeber VBA has the split function.

I got the following link by searching in google. Not sure which version of office you are using.

http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx

This link has the split function.




回答2:


You can use the Split() method or for more complex matches, you can use the "vbscript.regexp" object:

Sub NewRegex()
    Dim reg
    Dim matches, match, tmpStr As String

    Set reg = CreateObject("vbscript.regexp")
    tmpStr = "blah bla ...."

    With reg
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "your regex pattern goes here"
        .Global = True
    End With

    Set matches = reg.Execute(tmpStr)

    For Each match In matches
        MsgBox match
    Next mt

End Sub

Here's a tutorial on using regex from VBA: Using Regular Expressions (RegExp) in Excel




回答3:


VBA split function right from MS's page

http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx



来源:https://stackoverflow.com/questions/1112806/tokenizing-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!