[SOVLED] Extract specific number from string

Discussions and Tech Support related to website data extraction, screen scraping and data mining using iMacros.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

[SOVLED] Extract specific number from string

Post by DDon » Tue Oct 18, 2016 2:15 pm

Windows 10 64 bit
Firefox 48.0.2 32bit
iMacro 8.9.7

The string example is:

Code: Select all

"The repayable TSL debt as at 2016-10-16 is 4372.66."
I would like to extract the number 4372, it would be always next to the "is" word.
I do some google there is the code

Code: Select all

 SET !EXTRACT "The repayable TSL debt as at 2016-10-16 is 4372.66."
    SET !VAR1 EVAL("var x=\"{{!EXTRACT}}\"; x=x.replace(/\\D/g,\"\");")
    PROMPT {{!VAR1}}
But the result will be included the date as well. Can we have any solution? Thank you.
Last edited by DDon on Wed Oct 19, 2016 12:40 am, edited 1 time in total.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extract specific number from string

Post by chivracq » Tue Oct 18, 2016 4:07 pm

DDon wrote:

Code: Select all

Windows 10 64 bit
Firefox 48.0.2 32bit
iMacro 8.9.7
The string example is:

Code: Select all

"The repayable TSL debt as at 2016-10-16 is 4372.66."
I would like to extract the number 4372, it would be always next to the "is" word.
I do some google there is the code

Code: Select all

 SET !EXTRACT "The repayable TSL debt as at 2016-10-16 is 4372.66."
    SET !VAR1 EVAL("var x=\"{{!EXTRACT}}\"; x=x.replace(/\\D/g,\"\");")
    PROMPT {{!VAR1}}
But the result will be included the date as well. Can we have any solution? Thank you.
Yep, I'm not "good" with Global 'replace()' either, I prefer to use 'split()' (x2):

Code: Select all

 SET !EXTRACT "The repayable TSL debt as at 2016-10-16 is 4372.66."
    SET !VAR1 EVAL("var s='{{!EXTRACT}}'; var x,y,z; y=s.split(' is '); z=y[1].split('.'); z[0];")
    PROMPT {{!VAR1}}
(Not tested...)

The Principle is simple, you do first a 'split()' on " is " and you keep the 2nd part => "y[1]=4372.66.", then you do a 2nd 'split()' on "." (the Dot) and you keep the first part => "z[0]=4372".
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

Re: [SOVLED] Extract specific number from string

Post by DDon » Wed Oct 19, 2016 1:09 am

Thank you!

My problem is solved and first time ever I understand how the slip code work, Thanks for your explanation!
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: [SOVLED] Extract specific number from string

Post by chivracq » Wed Oct 19, 2016 9:36 am

DDon wrote:Thank you!

My problem is solved and first time ever I understand how the slip code work, Thanks for your explanation!
Well, good to hear, and glad to help... :D
And your Thread is simply perfect, with perfect Follow-up, useful for you, and probably useful for other Users... 8)

Oh...!, one thing I should add is that you usually need to activate '!ERRORIGNORE' before the 'EVAL()' Statement, because if your Extract ever returns "#EANF#" (Not Found) for any Reason, your Macro will stop with some RuntimeError that "y[1]" within the 'EVAL()' statement is not defined...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
saugi.bio@gmail.com
Posts: 1
Joined: Tue Jul 17, 2018 7:27 am

Re: [SOVLED] Extract specific number from string

Post by saugi.bio@gmail.com » Tue Jul 17, 2018 7:49 am

i want to ask if i want to scrape data from this web page

i have macro
TAG POS=1 TYPE=LI ATTR=TXT:Luas<SP>tanah<SP>:<SP>400<SP>m

if i just want to extract the number "400"
how is the script?
sorry my english not good,i am newbie in imacros script
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: [SOVLED] Extract specific number from string

Post by chivracq » Tue Jul 17, 2018 3:54 pm

saugi.bio@gmail.com wrote:i want to ask if i want to scrape data from this web page

i have macro

Code: Select all

TAG POS=1 TYPE=LI ATTR=TXT:Luas<SP>tanah<SP>:<SP>400<SP>m
if i just want to extract the number "400"
how is the script?
sorry my english not good,i am newbie in imacros script
CIM...! :mrgreen: (Read my Sig...)

Yeah, well..., same Principle like explained in this Thread for the original User, use 'split()' on " : " for example for the first 'split()', and then on the Space " " or " m" for the 2nd 'split()', or if you always get " m" at the end of the Extract, you could use 'replace()' instead..., what you prefer...

(Only be aware that you need to use "real" Spaces for 'split()' and/or 'replace()', I'm not sure it will work with "<SP>"...)

>>>

There is supposed to be a "short way" in your Case which is more of a "Trick", as you only want to keep the Number part in your String, using 'parseInt()', but I did some quick Testing in v8.8.2 for FF + v8.9.7 for FF, and I didn't get it to work as expected in both Versions, all I get is "NaN" in all Cases... (with 'parseInt()' + 'parseFloat()' + 'Number()' + "*1", with "<SP>" or real Spaces or even without any Spaces at all...), but even if it worked, I guess iMacros would get "confused" if by any chance some extra Number might be present in your Extract like in "Luas2 tanah..." or "...400 m2" for example..., then the 'split()' Method will be more "reliable" anyway...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply