[SOVLED] Extract specific number from string

Discussions and Tech Support related to website data extraction, screen scraping and data mining using iMacros.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

[SOVLED] Extract specific number from string

by DDon on Tue Oct 18, 2016 7:15 am

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 Tue Oct 18, 2016 5:40 pm, edited 1 time in total.
DDon
 
Posts: 11
Joined: Sat Aug 06, 2016 6:41 am

Re: Extract specific number from string

by chivracq on Tue Oct 18, 2016 9:07 am

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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6131
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: [SOVLED] Extract specific number from string

by DDon on Tue Oct 18, 2016 6:09 pm

Thank you!

My problem is solved and first time ever I understand how the slip code work, Thanks for your explanation!
DDon
 
Posts: 11
Joined: Sat Aug 06, 2016 6:41 am

Re: [SOVLED] Extract specific number from string

by chivracq on Wed Oct 19, 2016 2: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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6131
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)


Return to Data Extraction and Web Screen Scraping

Who is online

Users browsing this forum: No registered users and 3 guests

-->