string.gsub return 作为每个替换结果的不同变量

string.gsub return as a distinct variable for each replace result

输入:

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

你的模式中有这么多.,很难阅读,也不清楚你的目的是什么。

而是使用特定于格式的模式:

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)
  -- do whatever with m
end