R – VBS- String split multiple on multiple lines into array


I'm trying to create a function that will take a string which could be over multiple lines, e.g.:

"declare notThese
declare orThis

hello = $notThis@butthis$
butNot= $ButNotThis$

andDefNot = getDate()"

And search through it, pulling out {string1}'s from all parts like


and then pushing them into an array.

How would I archive this? Would it be through regex or is it simpler than that?

Also would it make a difference if the string renders on multiple lines like above?

Best Solution

You can do it through regex. Multi-line or not does not play a role in this case.

Function ExtractStrings(input)
  Dim re, matches, match, i, output

  Set re = new RegExp
  re.Pattern = "\$[^@]+@([^$]+)\$"
  re.Global = True

  Set matches = re.Execute(input)

  ReDim output(matches.Count - 1)

  i = 0
  For Each match in matches  
    output(i) = match.SubMatches(0)
    i = i + 1

  ExtractStrings = output
End Function
Related Question