number in a string

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
demac43
Newbie
Posts: 7
Joined: Wed Mar 09, 2005 9:58 pm
Location: Australia

number in a string

Post by demac43 » Wed Mar 09, 2005 10:05 pm

Maybe I haven't looked hard enuf, but cannot find a command that extracts a number from a string.
My string may be '5 inch' or '5 cm' and I wish to do calculations with it, so I only need the 5.

regards demac

:roll:

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed Mar 09, 2005 10:49 pm

Well, here's a script that removes non-numerics from a string. So maybe this will help:
http://www.mjtnet.com/forum/viewtopic.p ... ber+string

Or use VBScript's regular expressions. This script extracts all numerics from a string:

VBSTART
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
For Each Match in Matches
RetStr = RetStr & Match.Value
Next
RegExpTest = RetStr
End Function
VBEND
Let>MyString=Today is 9th March 2005
VBEval>RegExpTest("(\d+)","%MyString%"),numbers
MessageModal>numbers

If you run that without modification the message box will display 92005.
MJT Net Support
[email protected]

demac43
Newbie
Posts: 7
Joined: Wed Mar 09, 2005 9:58 pm
Location: Australia

Post by demac43 » Thu Mar 10, 2005 1:34 am

Thanks for the quick response.

I would like to point out that some macro programs have a command to do this (e.g.macromania) . However, I still prefer scheduler for many reasons.
Those macro programs that I've tried don't do enough string handling operations and also few involving character codes.

I miss the old Microsoft Basic which did it all.

regards demac

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Mar 10, 2005 3:39 am

demac43 wrote: I miss the old Microsoft Basic which did it all.

regards demac
Does VB include something like the old ASC and CHR$ ?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Mar 10, 2005 3:40 am

Oops.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts