我想解析命令的结果并将其保存到列表中:
Process Pid Mello No Status
122 523 GFAF_0 1 Running
125 512 GFAF_235 2 Running
128 987 GFAF_1 3 Stopped
预期产出:
[GFAF_0,GFAF_235,GFAF_1]
我希望GFAF_XYZ
(XYZ
是数字)的正则表达式,但我找不到办法。
此致 达米安
答案 0 :(得分:1)
我觉得你看起来像getopt
您需要根据输入格式指定选项
OptSpecList =
[
{host, $h, "host", {string, "localhost"}, "Database server host"},
{port, $p, "port", integer, "Database server port"},
{dbname, undefined, "dbname", {string, "users"}, "Database name"},
{xml, $x, undefined, undefined, "Output data in XML"},
{verbose, $v, "verbose", integer, "Verbosity level"},
{file, undefined, undefined, string, "Output file"}
].
然后致电getopt:parse/2:
getopt:parse(OptSpecList, ["-h", "myhost", "--port=1000", "-x", "file.txt", "-vvv", "dummy1", "dummy2"]).
你会得到元组
{ok,{[{host,"myhost"},
{port,1000},
xml,
{file,"file.txt"},
{dbname,"users"},
{verbose,3}],
["dummy1","dummy2"]}}
OR超简单解决方案
如果您以字符串形式接收数据,例如
"Process Pid Mello No Status 122 523 GFAF_0 1 Running 125 512 GFAF_235 2 Running 128 987 GFAF_1 3 Stopped"
按字词分组
L = string:tokens("Process Pid Mello No Status 122 523 GFAF_0 1 Running 125 512 GFAF_235 2 Running 128 987 GFAF_1 3 Stopped", " ").
["Process","Pid","Mello","No","Status","122","523","GFAF_0",
"1","Running","125","512","GFAF_235","2","Running","128",
"987","GFAF_1","3","Stopped"]
并检查元素是否包含“GFAF_”前缀
F = fun(Elem, Acc) ->
case string:find(Elem, "GFAF_", trailing) of
nomatch -> Acc;
_ -> [Elem|Acc]
end end,
lists:foldl(F, [], L).
您将收到列表
["GFAF_1","GFAF_235","GFAF_0"]