Regex Issues

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Monoxide3009
Posts: 65
Joined: 09 Apr 2018, 15:53

Regex Issues

11 Oct 2019, 17:00

Hey all,

I have some regex that works in a regex tester (https://regex101.com/) but does not work within AHK. I know where the issue lies, but not why or how to fix it. Any advice would be appreciated.

Text exmaple:

- CASE TYPE TOTALS:
-----------------
1 #1 PALLET
1 QUARTR CUT 63
- MARKS:
- FOR ULTIMATE SHIP TO:
MELBOURNE DISTRIBUTION CENTER

Regex example:

Code: Select all

RegExMatch(PDF_DATA,"^-----------------\R((.|\R)*)- MARKS:",PC_COUNT_TOTAL)
In the regex tester site, the output for PC_COUNT_TOTAL1 should be:
1 #1 PALLET
1 QUARTR CUT 63
But it comes up blank when running my code. I assume the site allows for ((.|\R*)*) but AHK doesnt for some reason. Any advice on how to resolve?


EDIT - my ultimate goal is to total the count (the number leading each line - there are a variable number of lines) - if you know of an easier way to accomplish this, it could bypass this original issue entirely.
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Regex Issues

12 Oct 2019, 04:27

the result of my tests:

the regex tester accepts the "^" as start of a(ny) line of a multiline pattern

AutoHotkey accepts the "^" as start of the haystack of a multiline pattern, even with the "m"option

Hubert
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: Regex Issues

12 Oct 2019, 06:27

This issue did cost me many headaches, too.
Take a look to https://www.autohotkey.com/boards/viewtopic.php?p=275713#p275713 and https://www.autohotkey.com/boards/viewtopic.php?p=275319#p275319.
The last one is in german, but following the step and Google translate help.

You have to parse with a loop through multiline text.
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Regex Issues

12 Oct 2019, 09:34

Kobaltauge wrote:
12 Oct 2019, 06:27
You have to parse with a loop through multiline text.
Here's how to do that:

Code: Select all

PDF_DATA =
(
- CASE TYPE TOTALS:
-----------------
1 #1 PALLET
1 QUARTR CUT 63
- MARKS:
- FOR ULTIMATE SHIP TO:
MELBOURNE DISTRIBUTION CENTER
)

RegExMatch(PDF_DATA,"-----------------\R((.|\R)*)- MARKS:", Match)
PC_COUNT_TOTAL := 0
loop, Parse, Match1, `n, `r
{
	RegExMatch(A_LoopField, "\d+", Count)
	PC_COUNT_TOTAL += Count
}
MsgBox, % PC_COUNT_TOTAL
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Regex Issues

12 Oct 2019, 19:04

You can simplify it further:

Code: Select all

Loop, parse, PDF_DATA, `n, `r
	var += substr(a_loopfield,1,a_loopfield ~= " ")
msgbox % var
Note: this is only possible because += treats blank variables as zero. The following won't work (though I believed it was identical!) :

Code: Select all

Loop, parse, PDF_DATA, `n, `r
	var := var + substr(a_loopfield,1,a_loopfield ~= " ")
msgbox % var
14.3 & 1.3.7
Monoxide3009
Posts: 65
Joined: 09 Apr 2018, 15:53

Re: Regex Issues

14 Oct 2019, 11:09

Thank you all for your suggestions. I can honestly say, I dont understand any of them (yet), but I will see what I can decipher today and see what I can get working.

One guy mentioned that it would catch on any match - I am aware of this. The pattern I gave will only ever have one match, and this is like the 40th pattern I have pulled from this PDF already, but I cant get this one to work. My assumption was simply because I was trying to pull 2 or more lines of data instead of just 1.

EDIT - I was able to get my original regex to work. For some reason, AHK required me to escape the hyphens. Once I did that, everything worked fine as I had it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, DaveT1, Joey5, KolaBorat, mebelantikjaya, Thorlian and 170 guests