Using regex to extract specific word

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
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
vganchev
Posts: 3
Joined: Thu Jan 14, 2021 12:36 pm

Using regex to extract specific word

Post by vganchev » Thu Jan 14, 2021 12:53 pm

Hi,
I have extracted one variable in another csv file and stored it under column 20.
Now I'm using the same csv file and I'm trying to get regex at column 20 to separate the text.
The text I have stored under col 20 is: VIN: VF32DNFUF43307492

My code is:

Code: Select all

SET  !VAR2 EVAL("var s=\"{{!COL20}}\"; var re=/[\n\r].*VIN:\s*([^\r]*)/; var result = re.exec(s); result[0];")
The expected result is to get only the VIN code which is: VF32DNFUF43307492
Instead of that I receive the following in the output csv file: "EVAL(""var s=\""{{!COL20}}\""; var re=/[\n\r].*VIN:\s*([^\r]*)/; var result = re.exec(s); result[0];"")"

Also I notice that we received a way more double quotes.
How to get only the VIN code?

version of the iMacros: 10.4.28.10.74
Thanks in advance!
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Using regex to extract specific word

Post by chivracq » Thu Jan 14, 2021 3:36 pm

vganchev wrote:
Thu Jan 14, 2021 12:53 pm
Hi,
I have extracted one variable in another csv file and stored it under column 20.
Now I'm using the same csv file and I'm trying to get regex at column 20 to separate the text.
The text I have stored under col 20 is: VIN: VF32DNFUF43307492

My code is:

Code: Select all

SET  !VAR2 EVAL("var s=\"{{!COL20}}\"; var re=/[\n\r].*VIN:\s*([^\r]*)/; var result = re.exec(s); result[0];")
The expected result is to get only the VIN code which is: VF32DNFUF43307492
Instead of that I receive the following in the output csv file: "EVAL(""var s=\""{{!COL20}}\""; var re=/[\n\r].*VIN:\s*([^\r]*)/; var result = re.exec(s); result[0];"")"

Also I notice that we received a way more double quotes.
How to get only the VIN code?

version of the iMacros: 10.4.28.10.74
Thanks in advance!

'REGEX'..., fouff...!, oh-yeah...!!? :shock:

If I understand correctly that, from "VIN: VF32DNFUF43307492", you only want to keep "VF32DNFUF43307492", then there are easier Solutions than using 'REGEX', ah-ah...!

A few Examples...:

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.replace('VIN: ',''); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.split(' '); z[1];")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.substr(5); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_
Choose the one you prefer..., I would go for the 'split()' one, I think...
'split()' is very "powerful", it can usually do everything that 'REGEX' does, + in 'search()', 'match()', Global 'replace()', etc... 8)

Let me know if you still get the Double Quotes, and I'll "adapt" my 3 Solutions to get rid of them if you don't come out by yourself...
- (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...
vganchev
Posts: 3
Joined: Thu Jan 14, 2021 12:36 pm

Re: Using regex to extract specific word

Post by vganchev » Thu Jan 14, 2021 4:37 pm

chivracq wrote:
Thu Jan 14, 2021 7:34 pm
'REGEX'..., fouff...!, oh-yeah...!!? :shock:

If I understand correctly that, from "VIN: VF32DNFUF43307492", you only want to keep "VF32DNFUF43307492", then there are easier Solutions than using 'REGEX', ah-ah...!

A few Examples...:

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.replace('VIN: ',''); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.split(' '); z[1];")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var z=c20.substr(5); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_
Choose the one you prefer..., I would go for the 'split()' one, I think...
'split()' is very "powerful", it can usually do everything that 'REGEX' does, + in 'search()', 'match()', Global 'replace()', etc... 8)

Let me know if you still get the Double Quotes, and I'll "adapt" my 3 Solutions to get rid of them if you don't come out by yourself...
Hi and thanks for your proposal.
I start thinking how to avoid using regex and find other way and it's working good.
SET !VAR2 EVAL("'{{!COL2}}'.replace("VIN:","")")
Last edited by vganchev on Thu Jan 14, 2021 8:44 pm, edited 2 times in total.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Using regex to extract specific word

Post by chivracq » Thu Jan 14, 2021 7:34 pm

vganchev wrote:
Thu Jan 14, 2021 4:37 pm
Hi and thanks for your proposal.
I start thinking how to avoid using regex and find other way and it's working good.
SET !VAR2 EVAL("'{{!COL2}}'.replace("VIN:","")")

Hum, you don't need to quote your OP again, that doesn't really help for the Readability of the Thread... :idea:

OK, nice to hear that it works, and your "other way" is the same like my 'replace()' Solution, using some slightly different and simplified Syntax...

The Syntax I use is deliberately not simplified and is easy to use with iMacros and to reuse and to extend to much complexer and much longer Expressions, and also to debug Step by Step inside the 'EVAL()'... And it usually takes care "automatically" of Double Quotes and Special Chars without the Need to escape them...

I would have expected for example that you would have needed to escape the Double Quotes inside your 'replace()'... But OK, if it works, then that's "good enough", I would think... :wink:

As an Example, if you had needed to remove the double Quotes from your Input in 'Col_20', this could have been done like, and applied to the 'replace()' Solution as you seem to prefer this one, and also keeping the Space like you do...:

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var x,y,z; x=c20.split('\"'); y=x[1]; z=y.replace('VIN:',''); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_
And this is just one of many-many different Solutions... 8)
And if it was not working "directly", then you would be able to replace the final "z" by "x" and then "y" to debug the Expression and check what those Steps are doing...
- (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...
vganchev
Posts: 3
Joined: Thu Jan 14, 2021 12:36 pm

Re: Using regex to extract specific word

Post by vganchev » Thu Jan 14, 2021 8:46 pm

chivracq wrote:
Thu Jan 14, 2021 7:34 pm

Hum, you don't need to quote your OP again, that doesn't really help for the Readability of the Thread... :idea:

OK, nice to hear that it works, and your "other way" is the same like my 'replace()' Solution, using some slightly different and simplified Syntax...

The Syntax I use is deliberately not simplified and is easy to use with iMacros and to reuse and to extend to much complexer and much longer Expressions, and also to debug Step by Step inside the 'EVAL()'... And it usually takes care "automatically" of Double Quotes and Special Chars without the Need to escape them...

I would have expected for example that you would have needed to escape the Double Quotes inside your 'replace()'... But OK, if it works, then that's "good enough", I would think... :wink:

As an Example, if you had needed to remove the double Quotes from your Input in 'Col_20', this could have been done like, and applied to the 'replace()' Solution as you seem to prefer this one, and also keeping the Space like you do...:

Code: Select all

SET VIN_Code EVAL("var c20='{{!COL20}}'; var x,y,z; x=c20.split('\"'); y=x[1]; z=y.replace('VIN:',''); z;")
PROMPT COL20:<SP>_{{!COL20}}_<BR>VIN_Code:<SP>_{{VIN_Code}}_
And this is just one of many-many different Solutions... 8)
And if it was not working "directly", then you would be able to replace the final "z" by "x" and then "y" to debug the Expression and check what those Steps are doing...

Actually didn't see that I take the whole thread. I edit my comment.
Thanks again for your proposal and improve my knowledge.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Using regex to extract specific word

Post by chivracq » Thu Jan 14, 2021 8:50 pm

vganchev wrote:
Thu Jan 14, 2021 8:46 pm
Actually didn't see that I take the whole thread. I edit my comment.
Thanks again for your proposal and improve my knowledge.

Yep, perfect, Thanks for that... :D
- (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