Author Topic: Working with two Match.1.1's  (Read 3897 times)

0 Members and 1 Guest are viewing this topic.

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
Working with two Match.1.1's
« on: December 29, 2016, 07:31:11 PM »
Hi, I'm having an issue getting RegEx to do what I want and would love some advice.

I'm basically trying to scrape numbers from two different websites and add them together.  The issue I'm having is that when I scrape the second site, the results in Match.1.1 are replaced when RegEX scrapes the second site.  I need to be able to use both matches from both scrapes at the same time.  How do I rename or set the first Match.1.1 as a variable?  I've tried MANY different things but obviously not the right one yet!  My problem when trying to set the result as a variable using {lastresult} is that lastresult is the full scrape and not the RegEX'd Match.1.1.

Thanks in advance!

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Working with two Match.1.1's
« Reply #1 on: December 29, 2016, 07:59:25 PM »
So far I can say from here - use {Match1.1} instead of {LastResult} as variable

1. Scrape
2. Result.SetVar  value1: {var.Name1}  value2:{Match1.1}
3. Scrape
4. Result.SetVar  value1: {var.Name2}  value2:{Match1.1}

Then you can work with var.name1 and var.name2

I hope this will help
« Last Edit: December 29, 2016, 08:08:01 PM by Kalle »
***********  get excited and make things  **********

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
Re: Working with two Match.1.1's
« Reply #2 on: December 29, 2016, 09:33:34 PM »
Thank you SO much!  I knew i was doing it incorrectly and thanks to your post I got it working!  My python math is working too :)  The only issue I am seeing is that there are many decimal places after doing the math.  Do you know how I can limit my result to only two decimal places (99999.99 instead of 99999.99999)?  I don't care about rounding, I'm happy to just cut the extras off :)

Thanks again, this is a great forum :)

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
Re: Working with two Match.1.1's
« Reply #3 on: December 30, 2016, 03:16:12 AM »
you can use regex to limit the amount of digits after the decimal

I wasn't sure if your number would always contain a decimal so I did a more advanced regex to make sure it would always return a match

it will work with

numbers with no decimals

numbers with 1 or more decimal placements

RegEx pattern:
Code: [Select]
(\d+\.\d\d|^\d+\.\d|^\d+)
Regex break down:

this will find any amount of digits before the decimal and only two digits after the decimal
Code: [Select]
\d+\.\d\d
This will find any amount of digits before the decimal and only one digit after the decimal if the number only has one digit after the RegEx
Code: [Select]
^\d+\.\d
this will find any amount of digits if the number does not contain a decimal
Code: [Select]
^\d+
this is used twice in the RexEx pattern, it is probably best described as an "or": this|or this|or this
Code: [Select]
|
parentheses are used to match a pattern instead of individual matches
Code: [Select]
()

 place the regex action after your python math action and use {Match.1.1} to use the cleaned up number
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.3.2-->
<command id="281" name="Decimal Cut off" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Results.RegEx</cmdType>
    <params>
      <param>(\d+\.\d\d|^\d+\.\d|^\d+)</param>
      <param />
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Dave

  • $upporter
  • Sr. Member
  • *****
  • Posts: 139
  • Karma: 31
    • View Profile
Re: Working with two Match.1.1's
« Reply #4 on: December 30, 2016, 03:53:15 AM »
Since you are already using python for the math, I would do the rounding with python, too.

Limiting to only two decimal places should work with: round(YourResult, 2)

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
Re: Working with two Match.1.1's
« Reply #5 on: December 30, 2016, 04:10:20 AM »
Since you are already using python for the math, I would do the rounding with python, too.

Limiting to only two decimal places should work with: round(YourResult, 2)

Nice call dave  8) , I didn't even think about python as an option. Python is not my strong suit, the only way I could get round(YourResult, 2) to work was using another action

Code: [Select]
PY.ExecString  result=round({LastResult}, 2)
is it possible to do it in the same PY.ExecString action as the math its self? sorry, like I said my python skills are non existence.

Dave

  • $upporter
  • Sr. Member
  • *****
  • Posts: 139
  • Karma: 31
    • View Profile
Re: Working with two Match.1.1's
« Reply #6 on: December 30, 2016, 04:47:20 AM »
is it possible to do it in the same PY.ExecString action as the math its self? sorry, like I said my python skills are non existence.

I guess instead of {LastResult} you could just insert your formula. Did you try that? E.G.:

Code: [Select]
PY.ExecString  result=round({var.Name1}*{var.Name2}, 2)
« Last Edit: December 30, 2016, 05:06:55 AM by Dave »

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
Re: Working with two Match.1.1's
« Reply #7 on: December 30, 2016, 12:39:12 PM »
That did the trick  8)

monkee

  • $upporter
  • Jr. Member
  • *****
  • Posts: 40
  • Karma: 4
    • View Profile
Re: Working with two Match.1.1's
« Reply #8 on: December 30, 2016, 03:16:54 PM »
I guess instead of {LastResult} you could just insert your formula. Did you try that? E.G.:

Code: [Select]
PY.ExecString  result=round({var.Name1}*{var.Name2}, 2)

Wow! Thank you both! 

The above technique worked perfectly.  I didn't even think of rounding in the math section I already had, duh!  In fairness, I only figured out you could do math in VC 2 days ago :)  This was a very clean solution (unlike most of what my commands look like, haha).  I should be able to apply this to several other commands that involve numbers too.  I can't wait to try.

This stuff is very fun to learn so thank you for the help when I get stuck :)