Capture elements of a string into variables

Capture elements of a string into variables

Post by Highlande » Fri, 03 Oct 2008 03:17:06


Hello all.

The output of c:\windows\system32\iisapp.vbs is this:

w3wp.exe 7156 0
246,428 K

I'd like to take that output string and extract 4 variables from it.
Something like this:

var1= w3wp.exe
var2 = 7156
var3 = 0
var4 = 246,428

(The fifth variable - "K" - I'm not concerned with.)

Any help in how to extract the variables would be greatly appreciated.
Thanks.

- Dave
 
 
 

Capture elements of a string into variables

Post by MV » Fri, 03 Oct 2008 03:57:23


You could do something like this:

sOutput = "w3wp.exe 7156 0
246,428 K"
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Global = True
oRegEx.Pattern = " +"
aVariables = Split(oRegEx.Replace(sOutput, " "))

Your variables are now the elements of the array aVariables, e.g.
aVariables(0), aVariables(1) etc.

 
 
 

Capture elements of a string into variables

Post by Highlande » Sun, 05 Oct 2008 23:10:33


> > 246,428 K
>
> > I'd like to take that output string and extra>t>4 variables from it.
>>> >o>ething like this:> >
> > var1= w3>p>exe
> > va>2>= 7156
> > var3 > 0> > > var4 = 246,428
>
> > (The fifth variable - "K" - I>m >o> concerned with.)
>
> > Any help in how to extract the variables would >e>greatly ap>re>i>ted.
> >>Th>nks.
>
> > - Dave
>
> You could >o >omething like this:
>
> sOutput = "w3wp.exe > >7156
> 246,428 K"> > Set oRegEx = CreateO>ject("VBScript.RegExp") >> oRegEx.Global = True
> oRegEx.Pattern = " +"
> >Va>iables = Split(oRegEx.Replace(sOutput, " "))
>
> Your variables ar> now the elements of the array aVariables, e.g.
> aVari>bl>s(0), aVariables(1) etc.- Hide quoted text -
>
> - Show quoted text -

Thanks! That worked like a champ!

Can you explain exactly how this pattern works... what it searches for
in the string?

oRegEx.Pattern = " +"
 
 
 

Capture elements of a string into variables

Post by MV » Sun, 05 Oct 2008 23:51:35


Thanks! That worked like a champ!

Can you explain exactly how this pattern works... what it searches for
in the string?

oRegEx.Pattern = " +"

==================

My very limited knowledge of regular expressions tells me that it searches
for one or more spaces.
 
 
 

Capture elements of a string into variables

Post by essk » Tue, 07 Oct 2008 22:50:27

If you have problem with regular expressions in VBS check this
program: regexCreator_v1_0

For people who don't have time to count letters ;)

Regards,
esska
 
 
 

Capture elements of a string into variables

Post by MV » Wed, 08 Oct 2008 06:39:01


Nice - Thank you for the tip!
 
 
 

Capture elements of a string into variables

Post by Dr J R Sto » Thu, 09 Oct 2008 01:53:23


No doubt that works. A more general solution could be to use RegExp
Match, recognising the fields rather than the gaps between them.

(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL: http://www.yqcomputer.com/ ;
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
 
 
 

Capture elements of a string into variables

Post by Monito » Thu, 09 Oct 2008 04:03:58


No doubt that works. A more general solution could be to use RegExp
Match, recognising the fields rather than the gaps between them.

================

Care to give a working example?