Simple RegEx error

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Simple RegEx error

26 Jan 2023, 12:40

I can't believe I'm having a problem with this very simple RegExMatch() call:

Code: Select all

#NoEnv
#SingleInstance force
#Warn

sHaystack := "abcyxw"
iMatch := RegExMatch(sHaystack, "^(?P<Begin>.+)y", Match)		
if iMatch
	sMatch := MatchBegin
else
	sMatch  := "(nothing)"
MsgBox % "iMatch=" iMatch "`nsMatch=""" sMatch """"
This gives the following warning: Warning: This variable has not been assigned a value.
Specifically: MatchBegin (a global variable)
---> 008: sMatch := MatchBegin

and the message box says:iMatch=1
sMatch=""
- instead of saying iMatch=1
sMatch="abc"
.

I tried using the other allowed syntax constructs, such as Mode 3 or numbered rather than named subpatterns, but to no avail. What am I missing?
RussF
Posts: 1282
Joined: 05 Aug 2021, 06:36

Re: Simple RegEx error

26 Jan 2023, 12:54

I'm not fluent with regex, but I can tell immediately from your warning message, as you should be able to, that you are trying to get data from the undefined variable MatchBegin when the output variable from your RegExMatch function is Match.

Russ
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Simple RegEx error

26 Jan 2023, 12:57

Sorry, my bad: I had a non-printable character before the variable name. (I remember having the same problem two years ago, when someone here pointed it out.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 97 guests