Xruptor | 2 Oct 17:55

Simple Question

Question: What is a good way to remove all Magic Characters from a string without distorting or breaking the accuracy of the original string?  I figure it has to do with the (%W) pattern.  However all my attempts usually break the string somehow or distort it.  My goal is to remove all possible magic characters from a string before parsing it in a string.find or any other string function.

I'm trying to parse chat information but people are bypassing the string.find function by adding special magic characters to their text strings.  Sometimes it breaks the string and sometimes the parse bypasses all find functions.

This is why I would like to strip all magic characters from a string before parsing ;)  Help is always appreciated.
Evan DeMond | 2 Oct 18:04

Re: Simple Question


On Thu, Oct 2, 2008 at 11:58 AM, Xruptor <derkyle <at> gmail.com> wrote:
Question: What is a good way to remove all Magic Characters from a string without distorting or breaking the accuracy of the original string?  I figure it has to do with the (%W) pattern.  However all my attempts usually break the string somehow or distort it.  My goal is to remove all possible magic characters from a string before parsing it in a string.find or any other string function.

I'm trying to parse chat information but people are bypassing the string.find function by adding special magic characters to their text strings.  Sometimes it breaks the string and sometimes the parse bypasses all find functions.

This is why I would like to strip all magic characters from a string before parsing ;)  Help is always appreciated.

Check out string.gsub. You can use "" for your replacement to make instances of the pattern go away.
http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub.
Jerome Vuarand | 2 Oct 18:31

Re: Simple Question

2008/10/2 Xruptor <derkyle <at> gmail.com>:
> Question: What is a good way to remove all Magic Characters from a string
> without distorting or breaking the accuracy of the original string?  I
> figure it has to do with the (%W) pattern.  However all my attempts usually
> break the string somehow or distort it.  My goal is to remove all possible
> magic characters from a string before parsing it in a string.find or any
> other string function.
>
> I'm trying to parse chat information but people are bypassing the
> string.find function by adding special magic characters to their text
> strings.  Sometimes it breaks the string and sometimes the parse bypasses
> all find functions.
>
> This is why I would like to strip all magic characters from a string before
> parsing ;)  Help is always appreciated.

If you want to keep the original string unchanged, what you want is
not stripping special characters, but escaping them. To do that you
can use the following function:

function escape(str)
  return (str:gsub("%W", "%%%1"))
end

Ben Kelly | 2 Oct 20:07

Re: Simple Question

Jerome Vuarand wrote:
> 2008/10/2 Xruptor <derkyle <at> gmail.com>:
>> This is why I would like to strip all magic characters from a string before
>> parsing ;)  Help is always appreciated.
> 
> function escape(str)
>   return (str:gsub("%W", "%%%1"))
> end

What Jerome said.
Of course, if you'd actually stayed connected to IRC rather than
quitting when your question wasn't answered /immediately/, you might
have had the answer even earlier.
	Ben

Petite Abeille | 2 Oct 18:54

Re: Simple Question


On Oct 2, 2008, at 5:58 PM, Xruptor wrote:

> This is why I would like to strip all magic characters from a string  
> before
> parsing ;)  Help is always appreciated.

"Sometimes, you want to change every plain occurrence of s1 to s2,  
without regarding any character as magic. "

-- Roberto Ierusalimschy, Programming in Lua, "Tricks of the Trade",  
December 2003
http://www.lua.org/pil/20.4.html

Cheers,

PA.


Gmane