如何提取命令的变量?

我想知道如何使用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 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"]]

我应该如何成功提取例如端口和app-pid?

点赞
用户2828480
用户2828480

你可以使用 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,了解如何创建自己的模式。 它几乎就像普通的正则表达式。

2018-07-10 14:35:06