0

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

${whatever}@{string1}$

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?

2 Answers 2

2

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
  Next

  ExtractStrings = output
End Function
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it via the Split function:

  Dim sLinesOfText As String
  sLinesOfText = "Insert multiple lines of text here"
  Dim aLines() As String
  Dim iLine As Integer
  iLine = 0
  
  aLines = Split(sLinesOfText, vbCrLf, , vbTextCompare)
  Do While iLine < UBound(aLines)
    Debug.Print aLines(iLine)
    iLine = iLine + 1
  Loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.