如何从字符串中获取整数?

yar*_*r83 1 excel vba

我从旧文件中抓取了文本,需要获取放置在字符串中的数字数据。

字符串看起来像:

"season: 1983 colony: 23 colony weight: 4 kg yeild: 12 kg
"season: 1983 colony:- colony weight: 5 kg yeild: 14 kg"
Run Code Online (Sandbox Code Playgroud)

我做了一个函数,它接受一个原始数据字符串并返回一个整数数组。

Function getClearBeeData(rawData As Variant) As Integer()
  Dim retValue(4) As Integer 'array where each found number stored
  Dim strTempString As String 'temporary string to hold current number
  Dim i, k As Integer 'i counter for original string, k counter for array position
  Dim token As Boolean 'token shows whether previous chars were number
  token = False
  
  For i = 1 To Len(rawData)   'go through original string
  
    If IsNumeric(Mid(rawData, i, 1)) Then   'if current char is numeric
      strTempString = strTempString & Mid(rawData, i, 1)  'add current char to remporary string
      token = True  'show that current position is within numbers
    ElseIf Mid(rawData, i, 1) = Chr(45) Then  'in string a symbol "-" can appear
      strTempString = "0"
      token = True
    ElseIf Not IsNumeric(Mid(rawData, i, 1)) And token = True Then  'if current char is not numeric and token shows that previous char was number
      retValue(k) = CInt(strTempString)   'convert temporary string to int and write in to the array
      k = k + 1   'go to next array position
      token = False   'switch token to show that current position is not within numbers
      strTempString = ""  'delete stored data from temporary string
    End If
    
  Next
  
  If Len(strTempString) > 0 Then
    retValue(k) = CInt(strTempString) 'if original string ended with numbers, write that numbers to array
  End If
  getClearBeeData = retValue
End Function
Run Code Online (Sandbox Code Playgroud)

测试子打印数据。

Sub printClearBeeData()
  Dim rawData As String
  Dim clearDataArr() As Integer
  Dim i As Integer
  rawData = "season: 1983 colony: 12 colony weight: - kg yeild: 16 kg"
  clearDataArr = getClearBeeData(rawData)
  For i = LBound(clearDataArr) To UBound(clearDataArr) - 1
    Debug.Print clearDataArr(i)
  Next
End Sub
Run Code Online (Sandbox Code Playgroud)

一切正常。我能做得更好吗?(因为我独自工作,没有人可以进行代码审查。)
我没有使用正则表达式,因为我不知道它们。

Jvd*_*vdV 5

由于您的字符串模式将始终相同(其中一个或多个子字符串可以用“-”表示),RegEx因此可以成为一种简单、易于实现的工具来检索这些子字符串。一个非常基本的例子,让你开始:

Sub Test()

Dim rawData As String, ClearBeeData as Object

rawData = "season: 1983 colony: 12 colony weight: - kg yeild: 16 kg"
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\d+|-"
    Set ClearBeeData = .Execute(rawData)
End With

For Each el In ClearBeeData
    Debug.Print Val(el)
Next

End Sub
Run Code Online (Sandbox Code Playgroud)

@Peh 提供的链接将为您提供大量信息,但这里有一些小说明:

  • .Global = True- 如果设置为False我们还可以检索第一个匹配项。当设置为True我们可以检索所有匹配项。
  • .Pattern = "\d+|-"- 在完整字符串中搜索的一种非常基本的模式,其中\d只是字符类的缩写,[0-9]+搜索至少一个字符(或更长)的子字符串。管道符号表示OR如果位置没有任何数字,我们可以搜索-替代。
  • .Execute(...)- 将返回找到的匹配项的 Matchcollection 类型对象。请注意,当找不到匹配项时,它会产生错误。当字符串具有相同的模式时,这里没什么大不了的,但为了将来参考,您可能希望.Test在我们尝试.Execute.
  • Val(el)- 由于返回集合中的所有元素都是文本,Val如果您愿意,我们可以将它们作为数字返回。很酷的功能是Val("-")将返回 0。因此对于上面的示例,您的结果将是:

    1983
    12
    0
    16
    
    Run Code Online (Sandbox Code Playgroud)

  • 喜欢您对此有效答案的其他有用评论+:)。仅供参考,可能对我(迟到的)答案也感兴趣。 (2认同)