News:

There is now a ChangeLog page on the wiki
New developments are added at the bottom of the page.

Main Menu

Working with two Match.1.1's

Started by monkee, December 29, 2016, 07:31:11 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

monkee

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

#1
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
***********  get excited and make things  **********

monkee

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

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:
(\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
\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
^\d+\.\d

this will find any amount of digits if the number does not contain a decimal
^\d+

this is used twice in the RexEx pattern, it is probably best described as an "or": this|or this|or this
|

parentheses are used to match a pattern instead of individual matches
()


place the regex action after your python math action and use {Match.1.1} to use the cleaned up number
<?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

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

Quote from: Dave 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)

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

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

#6
Quote from: PegLegTV on December 30, 2016, 04:10: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.:

PY.ExecString  result=round({var.Name1}*{var.Name2}, 2)

PegLegTV


monkee

Quote from: Dave on December 30, 2016, 04:47:20 AM
I guess instead of {LastResult} you could just insert your formula. Did you try that? E.G.:

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 :)