gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:08:14 24 4
gpt4 key购买 nike

我从旧文件中抓取了文本,需要将数字数据放在字符串中。
字符串看起来像:

"season: 1983 colony: 23 colony weight: 4 kg yeild: 12 kg
"season: 1983 colony:- colony weight: 5 kg yeild: 14 kg"
我做了一个函数,它接受一个原始数据字符串并返回一个整数数组。
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
测试子打印数据。
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
一切正常。我可以做得更好吗? (因为我一个人工作,没有人可以进行代码审查。)
我没有使用正则表达式,因为我不知道它们。

最佳答案

由于您的字符串模式总是相同的(其中一个或多个子字符串可以用“-”表示),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

@Peh 提供的链接将为您提供大量信息,但在这里需要做一些小评论:
  • .Global = True - 如果设置为 False我们还可以检索第一个匹配项。当设置为 True我们可以检索所有匹配项。
  • .Pattern = "\d+|-" - 在 \d 的完整字符串中搜索的非常基本的模式是字符类 [0-9] 的简称和 +搜索至少一个字符(或更长)的子字符串。管道符号表示OR所以如果该位置没有任何数字,我们可以搜索-或者。
  • .Execute(...) - 将返回找到的匹配项的 Matchcollection 类型对象。请注意,当找不到匹配项时会产生错误。当字符串具有相同的模式时,这没什么大不了的,但为了将来引用,您可能需要包含 .Test在我们尝试 .Execute 之前.
  • Val(el) - 由于返回集合中的所有元素都是文本,我们可以使用 Val如果您愿意,可以将它们作为数字返回。很酷的功能是Val("-")将返回 0。因此对于上面的示例,您的结果将是:
    1983
    12
    0
    16
  • 关于excel - 如何从字符串中获取整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629935/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com