Getting RegExMatch syntax right

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
don_24
Posts: 11
Joined: 18 Feb 2024, 05:48

Getting RegExMatch syntax right

Post by don_24 » 05 Mar 2024, 23:14

I have a bit of code that looks like this:

Code: Select all

sMatches := []
Loop, %standardCount% {
if RegExMatch(standard[A_Index], rxStrg)
   sMatches.Push(standard[A_Index])
}
standardCount is the number of items in an array containing words. For some reason I don't seem to be able to use standard.MaxIndex()

standard[1] is "case"

rxStrg is ^[c,][a;][sl][ei]

But "case" is not appended to the new array sMatches as I wanted.

My syntax must be wrong but I haven't been able to find the error. To do with ""s I think but I have experimented with that and no joy so far.

User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: Getting RegExMatch syntax right

Post by Datapoint » 06 Mar 2024, 00:21

don_24 wrote:
05 Mar 2024, 23:14
For some reason I don't seem to be able to use standard.MaxIndex()
Try the for-loop or loop like here. I would usually use a for-loop for an array. Both work.

Code: Select all

standard := ["case", "b", "c"]

for myKey, myVal in standard {
	MsgBox % myKey "`n" myVal
}

; Or use a % to force an expression. https://www.autohotkey.com/docs/v1/Variables.htm#percent-space
; Honestly if you are new to AHK, I would suggest learning v2 instead since it eliminates this confusing bit of syntax.
Loop, % standard.MaxIndex() {
	MsgBox % A_Index "`n" standard[A_Index]
}
The regular expression you have seems like it would work:

Code: Select all

for myKey, myVal in standard {
	if RegExMatch(myVal, "^[c,][a;][sl][ei]", x)
		MsgBox % myVal " found."
}

don_24
Posts: 11
Joined: 18 Feb 2024, 05:48

Re: Getting RegExMatch syntax right

Post by don_24 » 06 Mar 2024, 23:24

Thanks. The problem was that I had created the array in the main part of the code but was trying to query it in a function.

Post Reply

Return to “Ask for Help (v1)”