❓ > Regex question..

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Xpressd
Posts: 11
Joined: 23 Jun 2021, 12:22

❓ > Regex question..

Post by Xpressd » 01 Jul 2021, 16:50

strings:

Code: Select all

1 F2 K3341 RTLO149-20918 SPL O-RING KIT 19.94EA 19.94

Code: Select all

6 GB NKM4515QMV23 NEW-KIT 16.5X7Q 23KECONO 73.97KT 443.82
[code]

trying to RegExMatch the Price (19.94 or 73.97) 
using 

[code]
    foundvarpartprice := RegExMatch(firstline, "(\d+\.\d+)[A-Z]{0,3}\s\d+\.\d*", pprice)
    MsgBox, 0, , %foundvarpartprice%
    MsgBox, 0, , %pprice%
   partprice := pprice1
foundvarpartprice returns the location of a result but for some reason when I try to print the result it just comes up blank - - any idea what I am missing here? :wtf:

I have it working with another vendors invoice but for some reason it's not working here - I feel like I might be missing something very obvious

Code: Select all

firstline := clipboard
    linearray := StrSplit(firstline, A_Space)
    secondhalf := StrSplit(firstline, descriptionA[2])
    partnumber := linearray[2]
    foundvarprice := RegExMatch(firstline, "(\d+\.\d+)\s", pprice)
    foundvardesc := RegExMatch(firstline, "\n(.+)\s", pdesc)
    partdescription := pdesc1
    partquantity := linearray[4]
    partprice := pprice1
    UOM := linearray[3]
   
Last edited by Xpressd on 02 Jul 2021, 11:05, edited 1 time in total.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: ❓ > Regex question..

Post by boiler » 01 Jul 2021, 17:22

If you run your code as you’ve posted it (after defining the input variable), it doesn’t display a blank, it displays the overall match. To get just the price, you want the matching subpattern pprice1. If you put that in your MsgBox statement, it shows just the price. Run the following script to see the difference:

Code: Select all

firstline := "1 F2 K3341 RTLO149-20918 SPL O-RING KIT 19.94EA 19.94"
foundvarpartpriceposition := RegExMatch(firstline, "(\d+\.\d+)[A-Z]{0,3}\s\d+\.\d*", pprice)
MsgBox, % "Found position:`t" foundvarpartpriceposition "`n"
	. "Overall match:`t" pprice "`n"
	. "Part price:`t" pprice1

Xpressd
Posts: 11
Joined: 23 Jun 2021, 12:22

Re: ❓ > Regex question..

Post by Xpressd » 01 Jul 2021, 20:41

Oh, so you're saying it works for you that is so weird I will try recreating the macro or do it on another PC - thanks for your input.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: ❓ > Regex question..

Post by boiler » 01 Jul 2021, 21:03

The first script works as long as you define the input variable and use the correct output variable like the script I posted does.

You are doing something different in the second script. You have it needing to match a space (\s) following the price in that script, which isn’t the case in the input string. If you change it to needing to match a letter after the price, it works:

Code: Select all

firstline := "1 F2 K3341 RTLO149-20918 SPL O-RING KIT 19.94EA 19.94"
foundvarprice := RegExMatch(firstline, "(\d+\.\d+)\w", pprice)
MsgBox, % pprice1

Xpressd
Posts: 11
Joined: 23 Jun 2021, 12:22

Re: ❓ > Regex question..

Post by Xpressd » 02 Jul 2021, 11:37

boiler wrote:
01 Jul 2021, 21:03
The first script works as long as you define the input variable and use the correct output variable like the script I posted does.

You are doing something different in the second script. You have it needing to match a space (\s) following the price in that script, which isn’t the case in the input string. If you change it to needing to match a letter after the price, it works:

Code: Select all

firstline := "1 F2 K3341 RTLO149-20918 SPL O-RING KIT 19.94EA 19.94"
foundvarprice := RegExMatch(firstline, "(\d+\.\d+)\w", pprice)
MsgBox, % pprice1

Yeah, sorry I should of explained more -

The input variable gets defined when the user presses ` in front of the item line, it copies the line and saves it as that variable

the second script was just the last vendor I added, it worked great, each vendor has a different format for listing their items:
sometimes quantity is listed first before the part number or sometimes it is listed after the description etc..
so I have to make a template for each vendor to get their Invoice / PO / Cost of item, description etc ..
and then I can just inject that into their DB instead of typing out each invoice by hand

Code: Select all

If (KenworthFlag = 1)
{
    Click, Left, 1
    Sleep, 25
    Send, {Shift Down}{End}{Shift Up}
    Sleep, 25
    Send, {Control Down}{c}{Control Up}
    Sleep, 25
    Send, {Home}
    Sleep, 25
    firstline := clipboard
    wholeline := StrReplace(firstline, "$" , "")
    linearray := StrSplit(wholeline, A_Space)
    FoundvarEOD := RegExMatch(wholeline, "\s[A-Z][A-Z]\s\d*\d*\d", EOD)
    descriptionA := StrSplit(wholeline, [linearray[2] , EOD])
    secondhalf := StrSplit(wholeline, descriptionA[2])
    secondstring := secondhalf[2]
    secondlist := StrSplit(secondstring, A_Space)
    partnumber := linearray[2]
    partdescription := descriptionA[2]
    UOM := secondlist[2]
    partquantity := secondlist[3]
    partprice := secondlist[4]
    linetotal := secondlist[5]
}
If (FleetbrakeFlag = 1)
{
    Click, Left, 1
    Sleep, 25
    Send, {Shift Down}{End}{Shift Up}
    Sleep, 25
    Send, {Control Down}{c}{Control Up}
    Sleep, 25
    Send, {Home}
    Sleep, 25
    firstline := clipboard
    parray := StrSplit(firstline, A_Space)
    partnumber := parray[1]
    fouindvaruom := RegExMatch(firstline, "\s([A-Z]+)\s\d+\s", UOM)
    descriptionA := StrSplit(firstline, [parray[1] , UOM1])
    partdescription := descriptionA[2]
    foundvarqty := RegExMatch(firstline, "\w+\s(\d+)\s\$", pqty)
    foundvarprice := RegExMatch(firstline, "\$(.+\..+)\s", pprice)
    partquantity := pqty1
    partprice := pprice1
}
If (FortgarryFlag = 1)
{
    Click, Left, 1
    Sleep, 10
    Send, {Shift Down}{Down 2}{Shift Up}
    Sleep, 25
    Send, {Control Down}{c}{Control Up}
    Sleep, 100
    Send, {Home}
    Sleep, 10
    firstline := clipboard
    linearray := StrSplit(firstline, A_Space)
    secondhalf := StrSplit(firstline, descriptionA[2])
    partnumber := linearray[2]
    foundvarprice := RegExMatch(firstline, "(\d+\.\d+)\s", pprice)
    foundvardesc := RegExMatch(firstline, "\n(.+)\s", pdesc)
    partdescription := pdesc1
    partquantity := linearray[4]
    partprice := pprice1
    UOM := linearray[3]
}
If (GreggFlag = 1)
{
    Click, Left, 1
    Sleep, 10
    Send, {Shift Down}{Down 2}{Shift Up}
    Sleep, 25
    Send, {Control Down}{c}{Control Up}
    Sleep, 100
    Send, {Home}
    Sleep, 10
    line1 := clipboard
    firstline := RegExReplace(line1, "−", "-")
    firstline := RegExReplace(firstline, "\s+", A_Space)
    parray := StrSplit(firstline, A_Space)
    partnumber := parray[4]
    foundvarprice := RegExMatch(firstline, "(\d+\.\d+)\s", pprice)
    foundvardesc := RegExMatch(firstline, "\n(.+)\s", pdesc)
    partdescription := pdesc1
    partquantity := linearray[2]
    partprice := pprice1
    MsgBox, 0, , 
    (LTrim
    Part Number = %partnumber%
    Part Description = %partdescription%
    Part Quantity = %partquantity%
    Part Price = %partprice%
    
    )
}
I am just trying to add all our vendors and the newest vendor I am trying to add is just returning the blank message box when I try and grab the line item, the ( %foundvarpartprice% gives me the location of the find but %pprice1% and even %pprice% still is returning a blank message box)

you said you tried the regex with the string I posted, and it returned the result, Regex101 is showing me that it is valid as well so I am going to try and removing all \s and replacing them with A_Space because I think it is a formatting issue when I am grabbing the line directly from the PDF - maybe it's picking up a return line and not regexing that out properly in ahk

I dont have access to that exact script or invoice today so when I get back to work on Monday I will try entering that string as

Code: Select all

firstline := "1 F2 K3341 RTLO149-20918 SPL O-RING KIT 19.94EA 19.94"
and if it displays the price maybe it will be an easy fix by simply replacing all white space (\s) with ' A_Space ' ?

thank you so much for your insight and help so far - you are so wonderful roaming around the forums helping people it's just so great to see.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: ❓ > Regex question..

Post by boiler » 02 Jul 2021, 12:32

Xpressd wrote: so I am going to try and removing all \s and replacing them with A_Space because I think it is a formatting issue when I am grabbing the line directly from the PDF - maybe it's picking up a return line and not regexing that out properly in ahk
...
and if it displays the price maybe it will be an easy fix by simply replacing all white space (\s) with ' A_Space ' ?
No, you're missing the point of what I said. The price does not have a space after it, so having it match with either \s or A_Space would both be wrong. It would not produce a match and therefore would not find the price that way. Look at the input strings you provided. In both cases the price is followed by a letter (19.94EA and 73.97KT), not a space, which is why I replaced \s with \w. It would be more precise to follow it with [A-Z], but \w works. Do you understand why what your proposing will not produce a match?

Xpressd wrote: thank you so much for your insight and help so far - you are so wonderful roaming around the forums helping people it's just so great to see.
You're welcome. I'm glad to help.

Xpressd
Posts: 11
Joined: 23 Jun 2021, 12:22

Re: ❓ > Regex question..

Post by Xpressd » 10 Jul 2021, 22:00

boiler wrote:
02 Jul 2021, 12:32
Do you understand why what your proposing will not produce a match?
Yes, haha thank you boiler - you are THE MAN :bravo:

Post Reply

Return to “Ask for Help (v1)”