Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LUA字符串,删除非字母数字或空格_Lua_Pattern Matching_Gsub_Lua Patterns - Fatal编程技术网

LUA字符串,删除非字母数字或空格

LUA字符串,删除非字母数字或空格,lua,pattern-matching,gsub,lua-patterns,Lua,Pattern Matching,Gsub,Lua Patterns,我有客户输入,可能包括字母、数字或空格。例如: local customer_input = 'I need 2 tomatoes'; 或 但是,由于我的应用程序的性质,我可能会在customer_输入字符串中获得,*、@等。我想删除除空格以外的任何非字母数字字符 我试过这些: customer_input , _ = customer_input:gsub("%W%S+", ""); 这句话除了第一个字外,什么都没说 或 这一个实际上删除了每个单词

我有客户输入,可能包括字母、数字或空格。例如:

local customer_input = 'I need 2 tomatoes';

但是,由于我的应用程序的性质,我可能会在customer_输入字符串中获得,*、@等。我想删除除空格以外的任何非字母数字字符

我试过这些:

customer_input , _ = customer_input:gsub("%W%S+", ""); 
这句话除了第一个字外,什么都没说

这一个实际上删除了每个单词的空格和第一个字母

所以,我知道我做错了,但我不确定如何匹配字母数字+空格。我相信这一定很简单,但我还没有弄明白

非常感谢您的帮助

您可以使用

customer_input , _ = customer_input:gsub("[^%w%s]+", ""); 

图案细节

[^-匹配任何字符的否定字符类的开始,但: %字母数字 %s-a空格 ]+-一次或多次。 你可以用

customer_input , _ = customer_input:gsub("[^%w%s]+", ""); 

图案细节

[^-匹配任何字符的否定字符类的开始,但: %字母数字 %s-a空格 ]+-一次或多次。
你好@Wiktor,非常感谢!它就像一个符咒。我完全忘了用[…]来否定一个表达式。嗨@Wiktor,非常感谢!它就像一个符咒。我完全忘了用[…]来否定一个表达式。
customer_input , _ = customer_input:gsub("[^%w%s]+", "");