vba 标记字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1112806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tokenizing Strings
提问by stanigator
I have around 100 rows of text that I want to tokenize, which are alike the following:
我有大约 100 行要标记的文本,如下所示:
<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?
我无法使用 VBA 查找标记化函数。在 VBA 中标记此类字符串的最简单方法是什么?
采纳答案by msvcyc
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.
您可以逐行阅读并使用 split 功能按空格拆分单词和数字。我依稀记得 VBA 有拆分功能。
I got the following link by searching in google. Not sure which version of office you are using.
我通过谷歌搜索得到了以下链接。不确定您使用的是哪个版本的 office。
http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx
http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx
This link has the split function.
此链接具有拆分功能。
回答by Mitch Wheat
You can use the Split()
method or for more complex matches, you can use the "vbscript.regexp"
object:
您可以使用该Split()
方法或更复杂的匹配,您可以使用该"vbscript.regexp"
对象:
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
这是有关在 VBA 中使用正则表达式的教程:在 Excel 中使用正则表达式 (RegExp)
回答by Cuervo's Laugh
VBA split function right from MS's page
来自 MS 页面的 VBA 拆分功能
http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx
http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx