Page 1 of 1

Problem with RegExMatch

Posted: 24 Jul 2019, 06:35
by SEHot
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 ?

Re: Problem with RegExMatch

Posted: 24 Jul 2019, 07:50
by sinkfaze
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.

Re: Problem with RegExMatch

Posted: 24 Jul 2019, 07:57
by swagfag
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

Re: Problem with RegExMatch

Posted: 24 Jul 2019, 07:57
by sinkfaze
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.

Re: Problem with RegExMatch  Topic is solved

Posted: 24 Jul 2019, 07:59
by TheDewd

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
}

Re: Problem with RegExMatch

Posted: 24 Jul 2019, 09:17
by tmplinshi

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 : ""
}

Re: Problem with RegExMatch

Posted: 24 Jul 2019, 09:58
by SEHot
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!