Using regex in EVAL

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.

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.

Using regex in EVAL

by azbob on Fri May 05, 2017 9:55 pm

Configuration: Surface Pro 2, Win 10, Firefox 53, iMacros Standard Edition (x86) Version 11.0.246.4051
Hello,
I have a field that has a mixture of numbers and characters and I want to strip out non-numerics so I can compare with another numeric field. In example below I need to extract "2152" in order for the if statement to work.

'Return only digits
SET gla {{!COL7}}
PROMPT {{gla}}- This returns "2152 sqft"
SET !VAR0 EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")
PROMPT {{!VAR0}} - This returns "EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")"
SET Subjectgla {!VAR0}}
PROMPT {{Subjectgla}} - This returns "EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")"
SET avggla {{!COL14}}
PROMPT {{avggla}} _This returns "2178"
SET substories {{!COL9}}
SET !VAR1 EVAL("if ({{Subjectgla}} < {{avggla}}) ' smaller'; else if ({{Subjectgla}} > {{avggla}}) ' larger'; else ' same';")
PROMPT {{!VAR1}} - This returns a "blank"

I have tried many syntax combos on "replace" statement, but to no avail.
What am I doing wrong??

Thanks
azbob
 
Posts: 67
Joined: Mon Sep 21, 2009 11:16 am

Re: Using regex in EVAL

by chivracq on Sat May 06, 2017 2:52 pm

azbob wrote:Configuration:
Code: Select all
Surface Pro 2, Win 10, Firefox 53, iMacros Standard Edition (x86) Version 11.0.246.4051

Hello,
I have a field that has a mixture of numbers and characters and I want to strip out non-numerics so I can compare with another numeric field. In example below I need to extract "2152" in order for the if statement to work.

Code: Select all
'Return only digits
SET gla {{!COL7}}
PROMPT {{gla}}- This returns "2152 sqft"
SET !VAR0 EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")
PROMPT {{!VAR0}} - This returns "EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")"
SET Subjectgla {!VAR0}}
PROMPT {{Subjectgla}} - This returns "EVAL("var gla; gla=gla.replace(/\D/g,'');gla;")"
SET avggla {{!COL14}}
PROMPT {{avggla}} _This returns "2178"
SET substories {{!COL9}}
SET !VAR1 EVAL("if ({{Subjectgla}} < {{avggla}}) ' smaller'; else if ({{Subjectgla}} > {{avggla}}) ' larger'; else ' same';")
PROMPT {{!VAR1}} - This returns a "blank"


I have tried many syntax combos on "replace" statement, but to no avail.
What am I doing wrong??

Thanks

Very good that you try to follow your Vars using 'PROMPT', that's the correct way to (try to) debug your Script... :D

I don't like (hum..., = master, oops!) 'REGEX' but from your Example with "2152 sqft", you can very easily isolate the "2152" part either with 'replace()' indeed (but you don't even need the Global Switch), or with 'split()'...

With 'replace()':
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.replace(' sqft',''); z;")
PROMPT _{{Subject_gla}}_


With 'split()':
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
PROMPT _{{Subject_gla}}_


The 'replace()' Solution will work if you always have " sqft" in the String, and the 'split()' Solution will work if you always have a Space between the "2152" part and the "sqft" part.

There are probably a few other Solutions, with 'substr()' and 'substring()' for example if the "2152" part always contains 4 Digits, but I would need to know what may change and what will remain constant in the Format of your Data in '!COL7' to come up with a "Generic" Solution (without using 'REGEX') if the 2 Solutions I gave you don't work in all Cases...

Then for your Comparison, with the Goal to return "smaller", "larger" or "same", you can use:
Code: Select all
SET Comp EVAL("var sg='{{Subject_gla}}', ag='{{avggla}}'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)){z= ' larger';} else{z=' same';}; z;")
PROMPT _{{Comp}}_

The "x1" part on 'sg' and 'ag' (Multiplication by 1) is maybe/probably not needed, I added it just to make sure that those 2 Vars are "really" Numbers, especially 'Subject_gla' could still be a String after the Manipulation from the original String "2152 sqft"...

Notice as well the Underscores ("_") I use to surround 'PROMPT' Values, they are meant to make sure you don't have any leading/trailing Spaces or Soft Returns in your Data, which for example with the 'split(' ')' on the Space would return a different Result. If you notice that you still have some Spaces, you then need to remove them with 'trim()'...

EDIT:
2 Typos in both 'replace()' and 'split()' mini-Scripts...
Last edited by chivracq on Sun May 07, 2017 2:48 am, edited 2 times in total.
- (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: 5959
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Using regex in EVAL

by azbob on Sat May 06, 2017 6:16 pm

Hi,

Had some problems.

COL7 = "2052 sqft"
The split:
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
PROMPT _{{Subject_gla}}_ returned "_2052_" ??
The replace:
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.replace(' sqft',''); z;")
PROMPT _{{!COL7}}_ returns "_2052 sqft_"
PROMPT _{{Subject_gla}}_ returns "_2052_"
So those both worked as expected.

However on the comparison when I used replace or split:
avggla = 1220 and Subject_gla = 2052
SET comp EVAL("var sg='{{Subject_gla}}', var ag='{{avggla}}'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)) {z=' larger';}else {z=' same';}; z;")
PROMPT _{{Subject_gla}}_ This returned "_2052_"
PROMPT _{{comp}}_ This returns "_same_";
Since sg(2052) is larger than ag(1220) the correct answer should have been larger.

It appears the var sg='{{Subject_gla}}' and var ag='{{avggla}}'; did not get work correctly.

If I instead do this:
SET Comp EVAL("var sg='2052', ag='1220'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)){z=' larger';} else {z=' same';}; z;")
PROMPT _{{Comp}}_ Returns "_larger_"


Thanks,
Bob
azbob
 
Posts: 67
Joined: Mon Sep 21, 2009 11:16 am

Re: Using regex in EVAL

by chivracq on Sun May 07, 2017 3:37 am

azbob wrote:Hi,

Had some problems.

COL7 = "2052 sqft"
The split:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split('  '); z[0];")
 PROMPT _{{Subject_gla}}_  returned "_2052_" ??

The replace:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.replace(' sqft',''); z;")
PROMPT _{{!COL7}}_    returns "_2052 sqft_"
PROMPT _{{Subject_gla}}_  returns "_2052_"

So those both worked as expected.

However on the comparison when I used replace or split:
Code: Select all
avggla = 1220 and Subject_gla = 2052
SET comp EVAL("var sg='{{Subject_gla}}', var ag='{{avggla}}'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)) {z=' larger';}else {z=' same';}; z;")
PROMPT _{{Subject_gla}}_ This returned "_2052_"
PROMPT _{{comp}}_ This returns  "_same_"; 

Since sg(2052) is larger than ag(1220) the correct answer should have been larger.

It appears the var sg='{{Subject_gla}}' and var ag='{{avggla}}'; did not get work correctly.

If I instead do this:
Code: Select all
SET Comp EVAL("var sg='2052', ag='1220'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)){z=' larger';} else {z=' same';}; z;")
PROMPT _{{Comp}}_  Returns "_larger_"


Thanks,
Bob

Oh yep, there were 2 Typos in the 'replace()' and 'split()' mini-Scripts about '!COL7' instead of '!COL1' and a semi-colon instead of a comma... (Can happen if I cannot test with the "real" Data/Site...)

But you didn't take my original 'Comp' Statement and introduced a mistake in "your" 'Comp' Statement by adding a "var" which is not needed... + I see a few Spaces which shifted around, though that shouldn't make a Difference, I think... My original 'Comp' Statement looks OK to me... But it relies on one of "my" 'Subject_gla' Definitions + your 'avggla' Definition, you didn't post the full Script so I cannot see if stg is missing...

Your full Script should now look like this:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
SET avggla {{!COL14}}
SET substories {{!COL9}}
SET Comp EVAL("var sg='{{Subject_gla}}', ag='{{avggla}}'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)){z= ' larger';} else if((sg*1)==(ag*1)){z=' same';} else{z='PROBLEM...!';}; z;")
PROMPT gla:<SP>_{{!COL7}}_<BR>Subject_gla:<SP>_{{Subject_gla}}_<BR><BR>avggla:<SP>_{{avggla}}_<BR>substories:<SP>_{{substories}}_<BR><BR>Comp:<SP>_{{Comp}}_

I added an extra Check on "sg=ag" to make sure that the Statement gets evaluated, but it should work, well..., if I didn't make any Typo again...!
- (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: 5959
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Using regex in EVAL

by azbob on Sun May 07, 2017 6:28 pm

Hi chivracq,
My bad...I shot myself in the foot and led you down the wrong path as I overlooked that both number fields had separating commas ie: 2,052 sqft and 1,220. Your Compare returns PROBLEM.
Sorry.

So when I did this to strip out commas:
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
Subject_gla Returns 2,052
SET Subject_gla EVAL("var c7='{{Subject_gla}}'; var z=c7.split(',').join(''); z;")
Returns 2052
SET avggla EVAL("var c7='{{!COL14}}'; var z=c7.split(',').join(''); z;")
Returns 1220
Your compare returns larger.

Is there a way to strip out all non-numerics From Col7 in one expression rather than two, as i tried to do with my original regex expression??

I tried this:
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' ').split(',').join(' '); z;")
but it returns it strips sqft but leaves comma 2,052

Thanks,
Bob
azbob
 
Posts: 67
Joined: Mon Sep 21, 2009 11:16 am

Re: Using regex in EVAL

by chivracq on Mon May 08, 2017 2:30 am

azbob wrote:Hi chivracq,
My bad...I shot myself in the foot and led you down the wrong path as I overlooked that both number fields had separating commas ie: 2,052 sqft and 1,220. Your Compare returns PROBLEM.
Sorry.

So when I did this to strip out commas:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
Subject_gla  Returns 2,052
SET Subject_gla EVAL("var c7='{{Subject_gla}}'; var z=c7.split(',').join(''); z;")
Returns 2052
SET avggla EVAL("var c7='{{!COL14}}'; var z=c7.split(',').join(''); z;")
Returns 1220

Your compare returns larger.

Is there a way to strip out all non-numerics From Col7 in one expression rather than two, as i tried to do with my original regex expression??

I tried this:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' ').split(',').join(' '); z;")

but it returns it strips sqft but leaves comma 2,052

Thanks,
Bob

Ah...!, the Comma is new indeed, ah-ah...!
Hum, then I think that without the "x1" that I had added to make sure to convert the Data to Numbers would still have worked then, but alphabetically instead of arithmetically, as long as both Fields always have 4 Digits...

But yep, you can combine 2 'EVAL()' Statements together, so...:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var z=c7.split(' '); z[0];")
'Subject_gla  Returns 2,052
SET Subject_gla EVAL("var c7='{{Subject_gla}}'; var z=c7.split(',').join(''); z;")
'Returns 2052

... would become:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var x,y,z; y=c7.split(' '); z=y[0].split(',').join(''); z;")

What I do is declare 3 Vars (x,y,z) and "work" backwards to 'z' for the final Result by using 'x' and 'y' for intermediary Results, and in your Case, 2 Steps are enough and you don't even need 'x'...

If you always have Max 1 Comma, I would myself have used 'replace()' instead of your double 'split().join()'...:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var x,y,z; y=c7.split(' '); z=y[0].replace(',',''); z;")


And in both Cases, you can probably get a more "compact" Expression to work but I find "my" System with (x,y,z) for intermediary Results easier to follow and understand again for yourself in a few weeks if you want to modify stg or reuse the Statement for stg similar...

So your full Script would become:
Code: Select all
SET Subject_gla EVAL("var c7='{{!COL7}}'; var x,y,z; y=c7.split(' '); z=y[0].split(',').join(''); z;")
SET avggla EVAL("var c14='{{!COL14}}'; var z=c14.split(',').join(''); z;")
SET substories {{!COL9}}
SET Comp EVAL("var sg='{{Subject_gla}}', ag='{{avggla}}'; var z; if((sg*1)<(ag*1)){z=' smaller';} else if((sg*1)>(ag*1)){z= ' larger';} else if((sg*1)==(ag*1)){z=' same';} else{z='PROBLEM...!';}; z;")
PROMPT gla:<SP>_{{!COL7}}_<BR>Subject_gla:<SP>_{{Subject_gla}}_<BR><BR>avggla:<SP>_{{avggla}}_<BR>substories:<SP>_{{substories}}_<BR><BR>Comp:<SP>_{{Comp}}_
- (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: 5959
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: Using regex in EVAL

by azbob on Mon May 08, 2017 7:53 pm

Hi chivracq,
It works great.
Thanks for all your effort and hanging in in there.
Really appreciate you taking the time to walk me through the different solutions.

Another question if I may.
Why did you choose to use split or replace rather than a regex expression?
As on the surface regex could accomplish the same, but fewer characters.
Thanks, Bob
azbob
 
Posts: 67
Joined: Mon Sep 21, 2009 11:16 am

Re: Using regex in EVAL

by chivracq on Tue May 09, 2017 2:15 am

azbob wrote:Hi chivracq,
It works great.
Thanks for all your effort and hanging in in there.
Really appreciate you taking the time to walk me through the different solutions.

Another question if I may.
Why did you choose to use split or replace rather than a regex expression?
As on the surface regex could accomplish the same, but fewer characters.
Thanks, Bob

Good, glad to hear that it works... :D

Well, about 'REGEX', like I said in my first Reply: "I don't like (hum..., = master, oops!) 'REGEX'...", I find it complex to understand how it works and I never took the time and "Effort" to go digging into it in order to master it a bit correctly... :oops:

iMacros with 'EXTRACT' and 'EVAL()' is practically always about Data Extraction and Data Manipulation/Isolation and I've until now always managed to achieve what I need usually with 'split()' and 'replace()' (+ 'substr()' and 'indexOf()' sometimes), even a Global 'replace()' (which is 'REGEX' actually) can be done with 'split().join()' like you did. Even instead of 'SEARCH' and 'match()', I prefer to use 'split()' that I find easier and more powerful to use...

Same thing a bit with JavaScript...; I never took the time to go digging into JavaScript (except the few Functions I use in 'EVAL()') and I do all my Scripts in pure '.iim' and I've never used one single '.js' Script while I guess (some of) my Scripts do more complex "Things" than what I've ever seen on the Forum from what other Users do...

But OK, maybe those "Super Advanced Users" doing "crazy" Things in '.js' Scripts never need to reach out to the Forum and can solve their Pb's by themselves and even always find a Workaround when they encounter a Bug like I was doing "in my little corner" for about 6 years I think before I first came to the Forum 4 years ago to report some nasty Bug (still not solved, but I finally found a Workaround a few months ago) and then started answering Threads when I noticed I could find a Solution for most Threads on the Forum... :o
- (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: 5959
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)


Return to Scripting and Command Line Interface

Who is online

Users browsing this forum: No registered users and 4 guests

-->