【问题标题】:Manipulates String with VBA用 VBA 操作字符串
【发布时间】:2017-05-31 10:39:25
【问题描述】:

我正在尝试使用 VBA 代码分析字符串。 我读了一个看起来像这样的字符串:

myStringInput = "FRAG_INST = someValue,DR = otherValue, FRAG = anotherValue"

在我的代码中,我想根据字符串中读取的值关联一些变量,我想像这样初始化我的变量:

Dim dr, frag, fraginst As String

fraginst = someValue 
dr = otherValue
frag = anotherValue

我尝试过诸如 Trim/Split/InStr 组合之类的方法,但我总是得到错误的值。 我不能只使用“Mid”函数,因为值的长度可能会从一个执行变为另一个执行......

为了更清楚,我需要设计这样的功能

fraginst = NiceFunction("FRAG_INST",myStringInput)

它会返回“someValue”

有没有简单的方法来做我想做的事?

谢谢

【问题讨论】:

    标签: string vbscript logic


    【解决方案1】:

    此解决方案运行良好。也许你可以试试这个。不过我没有使用任何正则表达式。

    方法: 我首先用分隔符,(逗号)分割字符串。遍历每个数组元素并用'='分割每个元素。将值与 '=' 左侧的字符串进行比较,并在修剪后将值返回到 '=' 的右侧。 需要中间功能。

    myStringInput = "FRAG_INST = someValue,DR = otherValue, FRAG = anotherValue"
    fraginst = NiceFunction("FRAG_INST",myStringInput)
    MsgBox fraginst
    
    Function NiceFunction(str1, str2)
        tempArr1 = Split(str2,",")
        For i=0 To UBound(tempArr1)
            tempArr2 = Split(tempArr1(i),"=")
            If StrComp(Trim(tempArr2(0)),str1,1)=0 Then
                NiceFunction = Trim(tempArr2(1))
                Exit For
            End If
        Next
    End Function
    

    【讨论】:

    • 乐于助人:)
    【解决方案2】:
    Function NiceFunction( varName, inputString )
    Dim match
        With CreateObject("VBScript.RegExp")
            .IgnoreCase = True
            .Global = False
            .Pattern = "(?:^|,)\s*" & varName & "\s*=\s*([^,]*)"
            For Each match in .Execute( inputString )
                NiceFunction = match.subMatches.Item(0)
            Next 
        End With 
    End Function
    

    您可以使用RegExp 对象来提取您需要的部分字符串。

    【讨论】:

    • 感谢您的帮助,我没想过使用正则表达式
    【解决方案3】:

    您应该使用dictionary,而不是坚持需要在运行时动态创建变量的“单个”变量(DR,...):

    Option Explicit
    
    Function s2d(s)
      If IsEmpty(gR) Then
         Set gR = New RegExp
         gR.Global = True
         gR.Pattern = "(\w+)\s?=\s?(\w+)"
      End If
      Set s2d = CreateObject("Scripting.Dictionary")
      Dim m
      For Each m In gR.Execute(s)
          If s2d.Exists(m.SubMatches(0)) Then
             '  Error: dup key
          Else
             s2d.Add m.SubMatches(0), m.SubMatches(1)
          End If
      Next
    End Function
    
    Function qq(s)
      qq = """" & s & """"
    End Function
    
    Dim gR ' "static" in s2d()
    Dim s : s = "FRAG_INST = someValue,DR = otherValue, FRAG = anotherValue"
    If 0 < WScript.Arguments.Count Then s = WScript.Arguments(0)
    Dim d : Set d = s2d(s)
    Dim k
    For Each k In d.Keys()
        WScript.Echo qq(k), "=>", qq(d(k))
    Next
    If d.Exists("DR") Then WScript.Echo "DR:", d("DR") ' access 'single' var
    

    输出:

    cscript 44282497.vbs
    "FRAG_INST" => "someValue"
    "DR" => "otherValue"
    "FRAG" => "anotherValue"
    DR: otherValue
    

    附言

    我使用了RegExp,因为变量名必须是“单词”/匹配“\w+”并且您的示例数据值也是如此,而分隔符看起来像一团糟/创造性地使用空格。有关如何在 VBA 中使用 RegExps,请参阅 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多