0

I am trying to analyze a String with VBA code. I read a String that looks like that :

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

And in my code I would like to associate some variable according to the value read in the string, I would like to initialize my variables like that :

Dim dr, frag, fraginst As String

fraginst = someValue 
dr = otherValue
frag = anotherValue

I have tried things like Trim/Split/InStr combination but I always ended up with wrong values. I cannot just use "Mid" function because length of the values mays change from one execution to another...

To be clearer, I need to design function like this

fraginst = NiceFunction("FRAG_INST",myStringInput)

and it would return "someValue"

Is there an easy way to do what I want ?

Thanks

LostReality
  • 657
  • 2
  • 8
  • 33

3 Answers3

2

This solution is working fine. May be you can try this. I have not used any Regular expressions though.

Approach: I first splitted the string by delimiter ,(comma). Traversed through each of the array elements and splitted each element by '='. Compared the value to the string present on the left side of '=' and returned value to the right of '=' after trimming. Mid function was needed.

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
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
1

Instead of insisting on 'single' variables (DR, ...) which would need the dynamic creation of variables at run-time, you should use a dictionary:

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

Output:

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

P.S.

I used a RegExp, because variable names must be 'words'/match "\w+" and your sample data values dito, whereas the separators look like a mess/use spaces creatively. See here fore how to use RegExps in VBA.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
1
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

You can use a RegExp object to extract the part of the string you need.

MC ND
  • 69,615
  • 8
  • 84
  • 126