Search code examples
luagsublua-patterns

Remove all chars from a string except "a","b","c","d"


I have a string where I want to replace all chars and digits with "", except chars a,b,c, d.

Instead of having to write multiple lines of long code like in the example below, is there some other way to write this more efficient?

myString:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$%,]", "") --special chars
... --same for chars
... --same for digits

Solution

  • Use caret symbol ^ That is, [^abcd]

    Caret symbol negates your set.

    E.g., you can read it more carefully here.