regex subpattern help

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

regex subpattern help

Post by Xpressd » 10 Jul 2021, 22:07

first of all - to everybody looking though these forums attempting to help people with their coding problems - thank you 100x over, if the world was made up entirely of people like you it would literally be heaven on earth so thank-you for being you.

I am trying to make a program to help scrape MMA stats from a website -
there is a table on this wiki page I am trying to grab info from

Code: Select all

InputBox, inputname, Fighter Name, Fighter Name, , 250, 100, 500, 500
Sleep, 25
siteprefix := "https://en.wikipedia.org/wiki/"
fightername := StrSplit(inputname, A_Space)
first := fightername[1]
last := fightername[2]
fightername := first "_" last
URL := siteprefix fightername
UrlDownloadToFile, %URL%, %A_Temp%\StatSRC.html
Sleep, 25
FileRead, statstring, %A_Temp%/StatSRC.html
Sleep, 25
statstring := RegExReplace(statstring, "\s+", A_Space)
foundvarko := RegExMatch(statstring, "yes2.>(\d+)", wikistat)
MsgBox, 0, , % wikistat1
MsgBox, 0, , % wikistat2
/*
MsgBox, 64, DFSBYTHENUMBERS, 
(LTrim
%first% %last% has:
 %wikistat1% Victories by KO
 %wikistat2% Victories by Submission
 %wikistat3% Victories by decision
 %wikistat4% Victories by disqualifiation
)
*/
for some reason "% wikistat1" prints a result
but I am sure it supposed to be %wikistat1% that should print the result
along with %wikistat2%, %wikistat3%, %wikistat4%

what am I doing wrong :?: :(

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: regex subpattern help

Post by swagfag » 10 Jul 2021, 22:44

the regex defines only a single capturing group, so only one variable corresponding to that capturing group ever gets created. why do u expect variables corresponding to capturing groups 2, 3 and 4 to exist?

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

Re: regex subpattern help

Post by Xpressd » 12 Jul 2021, 19:09

swagfag wrote:
10 Jul 2021, 22:44
the regex defines only a single capturing group, so only one variable corresponding to that capturing group ever gets created. why do u expect variables corresponding to capturing groups 2, 3 and 4 to exist?
I was expecting a wikistat1 to = Match 1, wikistat2 = Match 2 and wikistat3 to be Match 3
like this:
Image

but your saying that variable relates to Capture Groups not to Matches
If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern (?P<Year>\d{4}) would be stored in MatchYear. If a particular subpattern does not match anything (or if the function returns zero), the corresponding variable is made blank.
I think I should just do some more reading

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: regex subpattern help

Post by swagfag » 13 Jul 2021, 02:01

based on that screenshot, u should have been expecting "a wikistat1 to = Group 1, wikistat2 = Group 1 and wikistat3 to be Group 1", which they do(look at it again)
the difference is ahk's RegExMatch() attempts to only match once, whereas the thing u got that screenshot from does so multiple times(eg JS regex flavor with the \g\ flag can do this)

Post Reply

Return to “Ask for Help (v1)”