如何提取命令的变量?

时间:2018-07-10 13:59:42

标签: lua

我想知道如何使用Lua提取传递给命令的变量。

假设我们有一个像这样的字符串:

  

[[“ E:/ League of Legends / RADS / projects / league_client / releases / 0.0.0.154 / deploy / LeagueClientUx.exe”“ --release = 0.0.0.124”“ --rads-product-directory = E :/ League of Legends / RADS / solutions / league_client_sln / releases / 0.0.0.124 / deploy /“” --remoting-auth-token = sometoken“” --respawn-command = LeagueClient.exe“”“ --respawn-display- name = League of Legends“” --app-port = 54584“” --install-directory = E:/ League of Legends /“” --app-name = LeagueClient“” --ux-name = LeagueClientUx“”- -ux-helper-name = LeagueClientUxHelper“” --log-dir = LeagueClient日志“” --bugsplat-name = league_client_riotgames_com“” --bugsplat-platform-id = EUW1“” --app-log-file-path = E:/ League of Legends / Logs / LeagueClient Logs / 2018-07-10T15-32-34_35052_LeagueClient.log“” --app-pid = 35052“” --no-proxy-server“]]

我如何成功提取例如port和app-pid?

1 个答案:

答案 0 :(得分:2)

您可以使用string.gmatch

local str = [["E:/League of Legends/RADS/projects/league_client/releases/0.0.0.154/deploy/LeagueClientUx.exe" "--release=0.0.0.124" "--rads-product-directory=E:/League of Legends/RADS/solutions/league_client_sln/releases/0.0.0.124/deploy/" "--remoting-auth-token=sometoken" "--respawn-command=LeagueClient.exe" "--respawn-display-name=League of Legends" "--app-port=54584" "--install-directory=E:/League of Legends/" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--bugsplat-name=league_client_riotgames_com" "--bugsplat-platform-id=EUW1" "--app-log-file-path=E:/League of Legends/Logs/LeagueClient Logs/2018-07-10T15-32-34_35052_LeagueClient.log" "--app-pid=35052" "--no-proxy-server"]]

for k,v in string.gmatch(str, "\"%-%-([^=]+)=([^\"]+)") do
  print(string.format("%s=%s", k, v))
end

local str = [["E:/League of Legends/RADS/projects/league_client/releases/0.0.0.154/deploy/LeagueClientUx.exe" "--release=0.0.0.124" "--rads-product-directory=E:/League of Legends/RADS/solutions/league_client_sln/releases/0.0.0.124/deploy/" "--remoting-auth-token=sometoken" "--respawn-command=LeagueClient.exe" "--respawn-display-name=League of Legends" "--app-port=54584" "--install-directory=E:/League of Legends/" "--app-name=LeagueClient" "--ux-name=LeagueClientUx" "--ux-helper-name=LeagueClientUxHelper" "--log-dir=LeagueClient Logs" "--bugsplat-name=league_client_riotgames_com" "--bugsplat-platform-id=EUW1" "--app-log-file-path=E:/League of Legends/Logs/LeagueClient Logs/2018-07-10T15-32-34_35052_LeagueClient.log" "--app-pid=35052" "--no-proxy-server"]]

local params = {}
for k,v in string.gmatch(str, "\"%-%-([^=]+)=([^\"]+)") do
  params[k] = v
end

print(params["app-pid"])
print(params["app-port"])

输出:

release=0.0.0.124
rads-product-directory=E:/League of Legends/RADS/solutions/league_client_sln/releases/0.0.0.124/deploy/
remoting-auth-token=sometoken
respawn-command=LeagueClient.exe
respawn-display-name=League of Legends
app-port=54584
install-directory=E:/League of Legends/
app-name=LeagueClient
ux-name=LeagueClientUx
ux-helper-name=LeagueClientUxHelper
log-dir=LeagueClient Logs
bugsplat-name=league_client_riotgames_com
bugsplat-platform-id=EUW1
app-log-file-path=E:/League of Legends/Logs/LeagueClient Logs/2018-07-10T15-32-34_35052_LeagueClient.log
app-pid=35052

http://rextester.com/JEYCF1205

另外,有关如何创建自己的模式,请参见Lua Patterns。就像正则表达式。