[Solved] RegExMatch ErrorLevel seems inconsistent with documentation Topic is solved

Report problems with documented functionality
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

[Solved] RegExMatch ErrorLevel seems inconsistent with documentation

16 Apr 2019, 09:27

EDIT: my bad. As @Ragnar points out below, I was catching the error incorrectly.

According to RegExMatch docs, the function can
throw an exception on failure (not to be confused with "no match found")
with ErrorLevel set like so:
Case 1: 0, which means that no error occurred.
Case 2: A string in the following form: Compile error N at offset M: description. In that string, N is the PCRE error number, M is the position of the offending character inside the regular expression, and description is the text describing the error.
Case 3: A negative number, (...)
The code below is designed to produce a compile error of the second type because the regex pattern is incomplete, and should cause the function to set ErrorLevel to an error string. However, ErrorLevel is set to "0", which is incorrect both according to the docs and to PCRE internals.

Code: Select all

	Try {
		match_position := RegexMatch("blah", "(?:incomplete[")
	}
	Catch e {
		MsgBox % ErrorLevel
	}
I verified that PCRE itself in fact returns an error string, not zero, by running the pcretest utility that ships with PCRE. Testing that regex on subject "abc", PCRE fails with this message:
Failed: missing terminating ] for character class at offset 14
Which confirms that the pattern cannot compile, being incomplete.

Haven't checked the AHK code but I'm assuming there's a test slightly off in there. Thought I'd report. Thanks in advance for taking a look.
Last edited by freespacing on 16 Apr 2019, 10:22, edited 2 times in total.
User avatar
Ragnar
Posts: 261
Joined: 30 Sep 2013, 15:25

Re: RegExMatch ErrorLevel seems inconsistent with documentation  Topic is solved

16 Apr 2019, 10:01

ErrorLevel is not intended to be used like this.

The correct way with ErrorLevel:

Code: Select all

match_position := RegexMatch("blah", "(?:incomplete[")
if ErrorLevel
	MsgBox % ErrorLevel
The correct way with try-catch:

Code: Select all

try
	match_position := RegexMatch("blah", "(?:incomplete[")
catch e
	MsgBox % e.message
freespacing
Posts: 97
Joined: 28 Sep 2016, 11:14
Contact:

Re: [Solved] RegExMatch ErrorLevel seems inconsistent with documentation

16 Apr 2019, 10:21

@Ragnar
You are completely right!
Thank you so much. My bad.

Return to “Bug Reports”

Who is online

Users browsing this forum: LibertyToad and 4 guests