代码之家  ›  专栏  ›  技术社区  ›  skkakkar

从字符串中获取适当的最大值

  •  0
  • skkakkar  · 技术社区  · 7 年前

    我的字符串是 su=45, nita = 30.8, raj = 60, gita = 40.8 . 这涉及到这样的问题 Extract maximum number from a string 我在利用 maxNums 函数和得到的结果是40.8,而我希望它是60。如果代码行中的一个修正可以得到我想要的结果,那么下面的代码将被复制以避免交叉引用。如果这个字符串包含所有带小数点的数字,那么我将得到正确的结果,但是考虑到来自外部源的数据可能有完整的数字。

    Option Explicit
    Option Base 0    '<~~this is the default but I've included it because it has to be 0
    
    Function maxNums(str As String)
        Dim n As Long, nums() As Variant
        Static rgx As Object, cmat As Object
    
        'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
        If rgx Is Nothing Then
            Set rgx = CreateObject("VBScript.RegExp")
        End If
        maxNums = vbNullString
    
        With rgx
            .Global = True
            .MultiLine = False
            .Pattern = "\d*\.\d*"
            If .Test(str) Then
                Set cmat = .Execute(str)
                'resize the nums array to accept the matches
                ReDim nums(cmat.Count - 1)
                'populate the nums array with the matches
                For n = LBound(nums) To UBound(nums)
                    nums(n) = CDbl(cmat.Item(n))
                Next n
                'test array
                'Debug.Print Join(nums, ", ")
                'return the maximum value found
                maxNums = Application.Max(nums)
            End If
        End With
    End Function
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Sam    7 年前

    您的代码有一两个问题。第一个问题是正则表达式不查找十进制数。如果你把它改成

    .Pattern = "\d+\.?(\d?)+"
    

    它会更好地工作。简而言之:
    \ D+=至少一个数字
    ?=可选点
    (d?)+=可选数字

    这不是一个防水的表达,但至少在某种程度上是可行的。

    第二个问题是不同十进制符号的潜在问题,在这种情况下,在处理之前需要进行一些搜索和替换。

        2
  •  0
  •   Alex K.    7 年前

    如果它总是 x=数 我认为循环遍历每个分隔值,然后读取 = 为了价值:

    Function MaxValue(data As String)
        Dim i As Long, value As Double
        Dim tokens() As String: tokens = Split(data, ",")
    
        For i = 0 To UBound(tokens)
            '// get the value after = as a double
            value = CDbl(Trim$(Mid$(tokens(i), InStr(tokens(i), "=") + 1)))
            If (value > MaxValue) Then MaxValue = value
        Next
    End Function
    
        3
  •  0
  •   Gary's Student    7 年前

    没有 Regex :

    Public Function maxNums(str As String) As Double
        Dim i As Long, L As Long, s As String, wf As WorksheetFunction, brr()
        Set wf = Application.WorksheetFunction
        L = Len(str)
    
        For i = 1 To L
            s = Mid(str, i, 1)
            If s Like "[0-9]" Or s = "." Then
            Else
                Mid(str, i, 1) = " "
            End If
        Next i
    
        str = wf.Trim(str)
        arr = Split(str, " ")
    
        ReDim brr(LBound(arr) To UBound(arr))
    
        For i = LBound(arr) To UBound(arr)
            brr(i) = CDbl(arr(i))
        Next i
    
        maxNums = wf.Max(brr)
    End Function
    

    enter image description here

    推荐文章