Like the you can see above, is there possible to match that like I can do with MUSHclients own triggers? I already know that I could just switch (orphine|violet) to (%w+) or a number of things. But I -really- want to match those two specific words and -only-..
It just makes the system a little bit more illusion safe and the functions that gets the captures later dont need extra checks.
local words = {["orphine"] = true, ["violet"] = true}
local foo = string.match("You eat orphine.", "You eat (%w+).")
if words[foo] then
-- we have a match
end
Option two:
re = rex.new("^You eat (orphine|violet)\.$")
local start, end = re:match("You eat orphine.")
if start then
-- we have a match
end
With option two, it's a good idea to compile the regexp only once (with rex.new), and reuse it each time you need it. Also, option two uses the same regexp library as MUSHclient (PCRE).
local words = {["orphine"] = true, ["violet"] = true}
local foo = string.match("You eat orphine.", "You eat (%w+).")
if words[foo] then
-- we have a match
end
This will fail with indexing a nil value if there is no match, so it should probably read:
local foo = string.match("You eat orphine.", "You eat (%w+).")
if foo and words[foo] then
-- we have a match
end
This annoyance is probably why Lua uses % in its regexps and not \.
I should also point out that if you are using "send to script" then MUSHclient already turns \\ into \ before even handing over to Lua, so in "send to script" you need to use \\\\ inside a Lua string, to get a single \ inside that string.
However if using a script file, you don't need to worry about that (but you would still need to use \\ to put a single \ inside a Lua string).
Another "gotcha" in "send to script" is that MUSHclient uses % to recognize wildcards (eg. %1 is the first wildcard) and Lua also uses %1 for things like string.gsub where %1 refers to the first "found group".
So in "send to script" you need to double % to %% for it to work properly.
For both of these reasons, scripting in a script file (or the script part of a plugin) can be less of a hassle.
Yes, I'm coding in Mudbot although I'm still using MUSH as my client..
And I've run into another problem that I cant work out...
It seems to me that it's impossible to add any sort of variable value into long strings.
I'm working on my own trigger parser, it's basic form looks something like this.
for k, v in pairs(tLines) do
for _, t in pairs(tTriggers) do
if rex.match(v, [[t.pattern]]) then
echo("Matched")
end
end
end
Think that explains what I want to do, but the problem is that anything inbetween [[ and ]] except [] are handled as string?
Edit: I'm guessing I could just add a longstring into the table, but then I run into another problem with the function I'm using to add triggers.. since it's basicly called: triggerAdd(pattern, otherstuff1, otherstuff2)
Like: triggerAdd([[^You (\d+) Points\.$]], 'echo("matched")', nil)
That would probably work, but I'd like to not have to use the [[ ]] in the triggerAdd call.. And rather in the function, and when I've tried that it adds it like this: [['^You (\d+) Points\.$']]
Because otherwise I'm back to the same problem I had before where I had to escape the "\" in my LUA code..
I guess I should be happy that it's just working, but I'd really want to make this basic/core functions in my new system perfect..
Like I sayd, making a trigger parser right now, and using PCRE now so that I can use matches like (orphine|violet) and get the captures to use. And I'm having a function to add these triggers into the table to check with the output from the mud.
So I need to either figure out how to use [[ ]] (longstrings) with an variable in it from the trigger table. Or another way of making captures like PCRE can do, or third. Just make every trigger add the pattern as a longstring to begin with, like table.insert(tTriggers, { pattern = [[^You (\d+) Points\.$]] }). OR escape each "\"..
The escaping thing only matters for strings that are actually visible in the code. String quoting - "", '', [[]] - are just ways for you, the programmer to tell the program what a string is. Once the string is assigned, the program already knows what the string looks like, internally. You'll notice that strings never have the "", '', [[]] included in the actual value unless you add them yourself, either. It's just a syntactic way to get your intent across to the program.
This is how I add my "so called" triggers into the table that checks against the mud's output. The problem I'm having is that the string that's in tTriggers table now does'nt match because it's like this: ^You (d+) Points.$
You showed me that I could use [[ ]] to fix that problem, but it seems to me that the only way to add a long string like that with my function is to supply it in the "call" to the function, like:
trigger_add([[^You (\d+) Points\.$]], nil, nil, "Replaced")
And I was hoping that I would'nt have to do that because (to me) it looks more ugly and possible harder for anyone else using this function to understand. I had hopes that I could do something like I have, and just make the insert do this: table.insert(tTriggers, { pattern = [[pattern]], send = send, script = script, replace = replace }) or something similiar..
Anyway I'm starting to think that it's either that or double "\" to escape the second backslash..
Thanks for your time and patience with me and this thread. It's much appreciated!
This is how I add my "so called" triggers into the table that checks against the mud's output. The problem I'm having is that the string that's in tTriggers table now does'nt match because it's like this: ^You (d+) Points.$
You showed me that I could use [[ ]] to fix that problem, but it seems to me that the only way to add a long string like that with my function is to supply it in the "call" to the function, like:
trigger_add([[^You (\d+) Points\.$]], nil, nil, "Replaced")
And I was hoping that I would'nt have to do that because (to me) it looks more ugly and possible harder for anyone else using this function to understand. I had hopes that I could do something like I have, and just make the insert do this: table.insert(tTriggers, { pattern = [[pattern]], send = send, script = script, replace = replace }) or something similiar..
Anyway I'm starting to think that it's either that or double "\" to escape the second backslash..
Thanks for your time and patience with me and this thread. It's much appreciated!
It's really the only way. This is something you have to deal with at the syntactic level. Once the string is parsed, there's no going back to fiddle with the escapes.
Pretty much what I suspected but I had small hopes that there was some way to add like a variable into the long strings, although thinking about it, it would'nt have worked either since the string is already parsed and the \ is removed.. Oh well, I'll deal with it.