Problem with RegExMatch Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SEHot
Posts: 3
Joined: 28 Jul 2015, 12:26
Location: UK
Contact:

Problem with RegExMatch

24 Jul 2019, 06:35

My code;

Code: Select all

GuiControl,,StatusText, Searching subject for file numbers
FoundPos := RegExMatch(Haystack,"O)\b\d{4,6}\.\d{1,4}\b|\b\d{4,6}\/\d{1,4}\b",FoundNumber)
FoundCount := FoundNumber.Count()
GuiControl,,StatusText, Found: %FoundCount%
The Haystack is
Re: 091795.0001 this also 055916.0002 and 96190.14 also 69190/11
The above code produces zero matches.
Using regex101.com Broken Link for safety
All four matches are found

Changing the RegEx

Code: Select all

GuiControl,,StatusText, Searching subject for file numbers
FoundPos := RegExMatch(Haystack,"O)\b\d{4,6}(\.|\/)\d{1,4}\b",FoundNumber)
FoundCount := FoundNumber.Count()
GuiControl,,StatusText, Found: %FoundCount%
I now get 1 match, but regex101.com still finds all four
Using VBScript also finds all four matches with both RegExs
Am I missing something ?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Problem with RegExMatch

24 Jul 2019, 07:50

regex101 uses an implementation of regex with a global flag, which tells the engine to keep matching until no more matches can be found. AHK does not.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with RegExMatch

24 Jul 2019, 07:57

the regexmatch object is a mess. Count() only tells u how many subpatterns(()) its matched not the number of total matches, hence 0 since u havent defined any subpatterns in the first regexp
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Problem with RegExMatch

24 Jul 2019, 07:57

Also, .Count returns the overall number of subpatterns in your regex, so it's returning what's expected based on your regexes. If you want to see the match, you should use .Value.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Problem with RegExMatch  Topic is solved

24 Jul 2019, 07:59

Code: Select all

#SingleInstance, Force

Haystack := "Re: 091795.0001 this also 055916.0002 and 96190.14 also 69190/11"

Pos := 0

While (Pos := RegExMatch(Haystack, "\b\d{4,6}(\.|\/)\d{1,4}\b", Match, Pos + 1)) {
	MsgBox, % Match
}
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Problem with RegExMatch

24 Jul 2019, 09:17

Code: Select all

Haystack := "Re: 091795.0001 this also 055916.0002 and 96190.14 also 69190/11"
Result := RegExMatchAll(Haystack, "\b\d{4,6}[./]\d{1,4}\b")
MsgBox % Result.MaxIndex()
MsgBox % Result.1

RegExMatchAll(ByRef Haystack, NeedleRegEx, SubPat := "", StartPos := 1) {
	arr := []
	while ( pos := RegExMatch(Haystack, NeedleRegEx, match, StartPos) ) {
		arr.push(match%SubPat%)
		StartPos := pos + StrLen(match)
	}
	return arr.MaxIndex() ? arr : ""
}
SEHot
Posts: 3
Joined: 28 Jul 2015, 12:26
Location: UK
Contact:

Re: Problem with RegExMatch

24 Jul 2019, 09:58

Thank you for all the replies. Working now.
No matter how many times I'd read the Help page for RegExMatch I just hadn't clocked that it would only find one match!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 120 guests