Regex and Function

Posted by Flelm on Thu 25 Aug 2005 02:12 AM — 5 posts, 24,531 views.

#0
This Regex and this function make MUSHClient crash.

^ ([A-Za-z]+) was in the sign of( the)? ([A-Za-z ]+)( \(retrograde\))?\.$

function Nativity (name, link, wildcards, extraneous)

if (wildcards[1] == "Sun") then
Note (wildcards[1] .. " Ray - " .. signs[wildcards[3]].damage_type .. " Damage")
elseif (wildcards[1] == "Moon" or wildcards[1] == "Tarox" or wildcards[1] == "Aapek" or wildcards[1] == "Papaxi" or wildcards[1] == "Eroee" or wildcards[1] == "Sidiak") then
Note (wildcards[1] .. " Ray - " .. planets[wildcards[1]][signs[wildcards[3]].sign_type])
end

end

This Regex and the same function does not cause a crash.

^ ([A-Za-z]+) was in the sign of( the)? ([A-Za-z ]+)\.$

What am I missing?
#1
Well, this Regex, and changing wildcards[3] to wildcards[2] worked. I still would like to know why that first Regex didn't work.

^ ([A-Za-z]+) was in the sign of (?:the)? ([A-Za-z ]+)(?: \(retrograde\))?\.$
#2
I've seen this problem before. When you have a variable number of captures, especially your optional capturing of "the" in this pattern, it causes the wildcards to shift in the array. You weren't taking that into account, and so using the (?:) to avoid the match fixes the number of entries in your wildcards, thus correcting your problem.
Australia Forum Administrator #3
I can reproduce the problem, which occurs in lua.dll, although I am not sure why. If I run it in the debugger I get a nice error message. Lua should not crash, and I am trying to reproduce it without needing MUSHclient, triggers etc., so far without luck.

Effectively you are doing something like this:


a = {} -- empty table

print (a [nil].b) -- index of nil into table, then index by b


However trying this in the command-line version gives:


stdin:1: attempt to index field `?' (a nil value)
stack traceback:
        stdin:1: in main chunk
        [C]: ?


It seems the problem is caused by the nil index into the array, but why that causes a MUSHclient crash is a mystery.


Australia Forum Administrator #4
I have determined the cause of the crash, which is described in the release notes for version 3.67. This is fixed in that version.