Scripting a regular expression creation

Posted by Shaun Biggs on Sun 27 May 2007 04:55 PM — 3 posts, 17,198 views.

USA #0
I'm trying to make an alias to set the prompt and create a trigger which matches on the prompt for Aardwolf. The only difficulty I'm having is with colour codes. Aard uses the @ symbol to denote a colour. The @ and any single character after it is eaten by the parser, except for @~- which are special characters. "@@" = "@", "@~" or "@-" = "~".

I have the following:
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> s="@CC@cc@@@~@C@@@-asdf"
> for w in string.gmatch(s, "(@.)") do
>>   if w == "@@" then
>>     print("@")
>>   elseif w == "@-" or w == "@~" then
>>     print("~")
>>   end
>> end
@
~
@
~
> 

But I would like to have a string where I'm adding the unmatched sections to the modified ones I'm printing. The example string would wind up being "Cc@~@~asdf". For some really strange reason, I'm having a hard time getting my brain around the last step of this :(
Russia #1
It looks like a job for string.gsub(). This produces the needed result:


function fixString(st) 
  return string.gsub(st, "(@.)", 
    function (m) 
      local t = {["@@"] = "@", ["@-"] = "~", ["@~"] = "~"}
      return t[m] or "" 
    end )
USA #2
That works perfectly. Thank you very much.