View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Parsing a string into an array?

On Mon, 30 Apr 2007 00:18:47 +0100, "Dave F." wrote:

Hi
I'm using HTTPRequest to download a page of stock prices from ADVFN.

The part I want is of the format:
prices[1]=25.25;prices[2]=2.15;prices[3]=100.5; etc...

As you can see it's already passing it to an array within the page
functions, but is downloaded as a string.

Any ideas how can I parse this info into an array in VBA?

I can split using the semi colon but not sure how to get the rest into
the array.

On a more general note, Does anyone know if there's a better way to
obtain parts of a web page data without downloading it all & have to
strip out the bits I want?

Thanks in advance

Dave F.


Here's one idea.

First replace the "prices...=" part of the string leaving a semicolon delimited
string of prices. This uses Regular Expressions.

If the line contains other data than what you have shown, the regex will need
modification. But it shouldn't be too difficult.

Then Split based on the semi-colon.

================================================== =
Option Explicit

Public Const sPrices As String =
"prices[1]=25.25;prices[2]=2.15;prices[3]=100.5"

Sub ParsePrices()
Dim Prices As Variant
Dim objRegExp As Object
Dim objMatches As Object
Dim i As Long

Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.MultiLine = True

objRegExp.Pattern = "prices\[\d+\]="
Prices = objRegExp.Replace(sPrices, "")
Prices = Split(Prices, ";")

'Show parsed items

For i = 0 To UBound(Prices)
Debug.Print i, Prices(i)
Next i

End Sub
==========================================
--ron