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
String Lua-使用模式提取字符串_String_Lua_Lua Patterns - Fatal编程技术网

String Lua-使用模式提取字符串

String Lua-使用模式提取字符串,string,lua,lua-patterns,String,Lua,Lua Patterns,我刚开始学习Lua模式 我有一根绳子 | 2 | 34 | 56 | 1 如何从字符串中提取数字 我可以手动解析字符串并排除所有“|”字符。但我相信使用Lua模式会简单得多 在这种情况下,模式有何帮助?如果您只想打印这些数字,最好的方法是: str = "|2|34|56|1" str:gsub("%d+", print) 否则,如果希望将数字存储在表中,则需要更长的方法: str = "|2|34|56|1" local tFinal = {} str:gsub( "%d+", functi

我刚开始学习Lua模式

我有一根绳子
| 2 | 34 | 56 | 1

如何从字符串中提取数字

我可以手动解析字符串并排除所有“|”字符。但我相信使用Lua模式会简单得多


在这种情况下,模式有何帮助?

如果您只想打印这些数字,最好的方法是:

str = "|2|34|56|1"
str:gsub("%d+", print)
否则,如果希望将数字存储在表中,则需要更长的方法:

str = "|2|34|56|1"
local tFinal = {}
str:gsub( "%d+", function(i) table.insert(tFinal, i) end)
table.foreach(tFinal, print)        -- This is only to verify that your numbers have been stored as a table.

如果您只想打印这些数字,最好的方法是:

str = "|2|34|56|1"
str:gsub("%d+", print)
否则,如果希望将数字存储在表中,则需要更长的方法:

str = "|2|34|56|1"
local tFinal = {}
str:gsub( "%d+", function(i) table.insert(tFinal, i) end)
table.foreach(tFinal, print)        -- This is only to verify that your numbers have been stored as a table.

谢谢如果存在一些字符串,那么解决方案是什么?比如
“|2 | 34 | a | 1 | ba”
否。我的意思是,如果字符串是
|2 | 34 | a | 1 | ba
,我也希望提取字符串。我需要提取
2
34
“a”
1
“ba”
这可能吗?@SatheeshJM,用
“[^ |]+”
代替
%d+“
。谢谢!如果存在一些字符串,那么解决方案是什么?比如
“|2 | 34 | a | 1 | ba”
否。我的意思是,如果字符串是
|2 | 34 | a | 1 | ba
,我也希望提取字符串。我需要提取
2
34
“a”
1
“ba”
这可能吗?@SatheeshJM,使用
“[^ |]+”
而不是
“%d+”