Search and Replace



Is there an easy way to apply a series of search and replace strings
to a piece of text, from a list or table?

Yes. Use StrongED and drag in a script like this, with the
replace table appropriately modified (presuming you have !lua
filerbooted).

#! lua
-- multiple search and replace
local replace = { -- edit this table
["seperate"] = "separate";
["supercede"] = "supersede";
["kernal"] = "kernel";
["old rubbish"] = "snappy phrase";
}
-- search for string in square brackets, replace with
-- string between equal sign and semicolon.
local transform = \(x,y)
for old,new in pairs(y) do
x = x:gsub(old,new)
end -- for
=> x
end -- function
local f = io.open(arg[1],"r")
local s = f:read "*all"
f:close()
io.write(transform(s,replace))

--
Gavin Wraith (gavin@xxxxxxxxxxxxxxx)
Home page: http://www.wra1th.plus.com/
.



Relevant Pages

  • Re: Scripting (ECMA script) solution for .NET app?
    ... You get the source as a string, you compile it to an in memory ... Lua is ok and the LuaInterface gives more control than ... JScript.NET on what objects are shown to the scripts. ... where ppp is the object I am testing, outputWB is WebPage and outputRTB is is a RichTextBox used in output panel. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Binary search trees (AVL trees)
    ... Lua uses a garbage collector and doing without it in C makes things harder. ... My current solution is that containers own the objects they contain. ... This semplifies things a lot but requires to handle cases where this is not appropriate (for example storing in the table just the pointer to a string and not the string itself): ...
    (comp.lang.c)
  • Re: Add a file to the DEP Exception List
    ... anyway i have this string: "LUA DisableNXShowUI". ... Windows Registry Editor Version 5.00 ...
    (microsoft.public.windowsxp.basics)
  • Re: Coming back to Basic after many years
    ... Lua has 'loadstring', which is a lazy variant. ... It compiles the ... contents of its string argument to a function of no arguments, ...
    (comp.sys.acorn.programmer)