string.gsub返回每个替换结果的独立变量

输入:

stringdata = '{"abcd://ipaddress/directory1/Images/subfolder/Image0.png","abcd://ipaddress/directory1/Images/subfolder/Image1.png","abcd://ipaddress/directory1/Images/subfolder/Image2.png","abcd://ipaddress/directory1/Images/subfolder/Image3.png","abcd://ipaddress/directory1/Images/subfolder/Image4.png"}'

源代码

string.gsub(stringdata, "(.....................%w%w................................%w)",print)

当前输出:

abcd://ipaddress/directory1/Images/subfolder/Image0.png
abcd://ipaddress/directory1/Images/subfolder/Image1.png
abcd://ipaddress/directory1/Images/subfolder/Image2.png
abcd://ipaddress/directory1/Images/subfolder/Image3.png
abcd://ipaddress/directory1/Images/subfolder/Image4.png

期望输出(其中以下每个结果都可以存储在不同的变量中)

directory1/Images/subfolder/Image0.png
directory1/Images/subfolder/Image1.png
directory1/Images/subfolder/Image2.png
directory1/Images/subfolder/Image3.png
directory1/Images/subfolder/Image4.png
点赞
用户1009479
用户1009479

由于您的模式中有太多几个“。”,难以阅读,不清楚您的目的是什么。

相反,使用针对格式特定的模式:

string.gsub(stringdata, "%w+://%w+/(%w+/%w+/%w+/%w+%.png)", print)

用这种方式,每个%w+代表什么更加清晰。


存储结果时,您使用了错误的函数。string.gsub用于替换,请使用string.gmatch代替:

for m in string.gmatch(stringdata, "%w+://%w+/(%w+/%w+/%w+/%w+%.png)") do
  print(m)
  -- 做任何需要的操作
end
2016-05-24 10:22:57