lua-users home
lua-l archive

RE: String replacement without regular expressions

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Jan Schütze wrote:
I wanted to ask if there is any way to escape (fast) all
magic characters in the string one wants to replace?
I don't know if it's fast enough for you, but this function
does what you want:
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
function plain2pat(s)
 s = string.gsub(s, "[[%%.*+?^$()-]", "%%%0")
 return (string.gsub(s, "%z", "%%z"))
end
allchars = ""
for i = 0, 255 do
 allchars = allchars .. string.char(i)
end
print(string.find(allchars, plain2pat(allchars)))
1 256
--
Aaron
http://arundelo.com
_________________________________________________________________
Interest Rates Fall Again! 430,000ドル Mortgage for 1,399ドル/mo - Calculate new payment http://www.lowermybills.com/lre/index.jsp?sourceid=lmb-9632-18679&moid=7581

AltStyle によって変換されたページ (->オリジナル) /