erlang使用正则表达式将字符串解析为数据类型

时间:2017-11-01 11:01:51

标签: regex erlang

我试图在erlang中创建一个解析器,希望识别字符串中的数据类型。搜索之后,我无法找到任何存在的问题:

  • 原始字符串:atom1,"string2,,\"\",",{tuple3, "s pa ces \"", {[test]},"_#",test},<<"binary4\",,>>">>, #{map5=>5, element=>{e1,e2}}, #record6{r1 = 1, r2 = 2} , <<300:16>>

  • 需要解析的字符串:"atom1,\"string2,,\\\"\\\",\",{tuple3, \"s pa ces \\\"\", {[test]},\"_#\",test},<<\"binary4\\\",,>>\">>, #{map5=>5, element=>{e1,e2}}, #record6{r1 = 1, r2 = 2} , <<300:16>>"

  • 预期输出:

    + number of params: 7
    + value ------> type"
        - atom1 ------> Atom
        - "string2,,\"\"," ------> String
        - {tuple3, "s pa ces \"", {[test]},"_#",test} ------> Tuple
        - <<"binary4\",,>>">> ------> Binary
        - #{map5=>5, element=>{e1,e2}} ------> Map
        - #record6{r1 = 1, r2 = 2} ------> Record
        - <<300:16>> ------> Binary
    

但我目前的代码没有按预期工作,这里是:

comma_parser(Params) ->
{ok, R} = re:compile("(\".*?\"|[^\",\\s]+)(?=\\s*,|\\s*$)"),
{match, Matches} = re:run(Params, R, [{capture, [1], list}, global]),
?DEBUG("truonggv1 - comma_parser: Matches: ~p~n", [Matches]),
[M || [M] <- Matches].

当前输出:

  + number of params: 14
  + value ------> type
    - atom1 ------> Atom
    - "string2,,\"\" ------> String
    - ",{tuple3, "s pa ces \"" ------> String
    - {[test]} ------> Tuple
    - "_#" ------> String
    - test} ------> Atom
    - "binary4\" ------> String
    - >> ------> Atom
    - #{map5=>5 ------> Map
    - element=>{e1 ------> Atom
    - e2}} ------> Atom
    - 1 ------> Atom
    - 2} ------> Atom
    - <<300:16>> ------> Binary

有人知道如何解决这个问题吗?

使用Params更新我的代码是需要解析的&#34;字符串&#34;我上面已经提到过:

check_params_by_comma(Params) ->
  case string:str(Params, ",") of
     0 ->
       Result = Params;
     1 ->
       Result = "param starts with character ',' ~n";
     _Comma_Pos ->
       Parse_String = comma_parser(Params),
       Result = "number of params: " ++ integer_to_list(length(Parse_String))
                ++ "\n\n\r\t value ------> type \n\r"
                ++ "\t*********************\n\r"
                ++ ["\t" ++ X ++ " ------> " ++ check_type(X) ++ "\n\r"|| X <- Parse_String]
  end,
  Result.

check_type(X) ->
  Binary = string:str(X, "<<"),
  String = string:str(X, "\""),
  Tuple = string:str(X, "{"),
  List = string:str(X, "["),
  Map = string:str(X, "#{"),
  case X of
    _ when 1 == Binary -> "Binary";
    _ when 1 == String -> "String";
    _ when 1 == Tuple -> "Tuple";
    _ when 1 == List -> "List";
    _ when 1 == Map -> "Map";
    _ -> "Atom"
  end.

comma_parser(Params) ->
  {ok, R} = re:compile("(\".*?\"|[^\",\\s]+)(?=\\s*,|\\s*$)"),
  {match, Matches} = re:run(Params, R, [{capture, [1], list}, global]),
  [M || [M] <- Matches].

1 个答案:

答案 0 :(得分:2)

我并不完全确定我理解你想要实现的目标,但是让我告诉你我对你的意见做了什么,让我们看看这对你有什么帮助。 您的情况似乎迫切地要求erl_scan:stringerl_parse:parse_exprs,这是我尝试过的第一件事。

这是我原始版本的解析:

-module(x).

-export([test/0, check_params_by_comma/1]).

test() ->
  Input =
    "atom1,\"string2,,\\\"\\\",\",{tuple3, \"s pa ces \\\"\", "
    "{[test]},\"_#\",test},<<\"binary4\\\",,>>\">>, "
    "#{map5=>5, element=>{e1,e2}}, #record6{r1 = 1, r2 = 2} , <<300:16>>",
  io:format("~p~n", [check_params_by_comma(Input)]).

check_params_by_comma(Params) ->
  {ok, Tokens, _} = erl_scan:string(Params ++ "."),
  {ok, Exprs} = erl_parse:parse_exprs(Tokens),
  Exprs.

当然不是全部,因为你想要一种不同的输出,但我们几乎就在那里。从原始问题复制演示文稿代码,我不得不使用erl_prettypr:format/1来呈现这些条款,最后我得到了类似的内容:

-module(x).

-export([test/0, check_params_by_comma/1]).

test() ->
  Input =
    "atom1,\"string2,,\\\"\\\",\",{tuple3, \"s pa ces \\\"\", "
    "{[test]},\"_#\",test},<<\"binary4\\\",,>>\">>, "
    "#{map5=>5, element=>{e1,e2}}, #record6{r1 = 1, r2 = 2} , <<300:16>>",
  io:format("~s~n", [check_params_by_comma(Input)]).

check_params_by_comma(Params) ->
  Parse_String = comma_parser(Params),
  "number of params: " ++ integer_to_list(length(Parse_String))
  ++ "\n\n\r\t value ------> type \n\r"
  ++ "\t*********************\n\r"
  ++ ["\t" ++ erl_prettypr:format(X) ++ " ------> " ++ check_type(X) ++ "\n\r"|| X <- Parse_String].

comma_parser(Params) ->
  {ok, Tokens, _} = erl_scan:string(Params ++ "."),
  {ok, Exprs} = erl_parse:parse_exprs(Tokens),
  Exprs.

check_type({Type, _, _}) -> atom_to_list(Type);
check_type({Type, _, _, _}) -> atom_to_list(Type).

我认为这应该足以解决您的问题,但作为奖励跟踪,让我使用iolists重构一下,以获得几乎所需的预期输出:

-module(x).

-export([test/0, check_params_by_comma/1]).

test() ->
  Input =
    "atom1,\"string2,,\\\"\\\",\",{tuple3, \"s pa ces \\\"\", "
    "{[test]},\"_#\",test},<<\"binary4\\\",,>>\">>, "
    "#{map5=>5, element=>{e1,e2}}, #record6{r1 = 1, r2 = 2} , <<300:16>>",
  io:format("~s~n", [check_params_by_comma(Input)]).

check_params_by_comma(Params) ->
  {ok, Tokens, _} = erl_scan:string(Params ++ "."),
  {ok, Exprs} = erl_parse:parse_exprs(Tokens),
  [
    io_lib:format("+ number of params: ~p~n", [length(Exprs)]),
    "+ value ------> type \n"
  | lists:map(fun format_expr/1, Exprs)
  ].

format_expr(Expr) ->
  io_lib:format(
    "\t- ~s ------> ~s~n",
    [erl_prettypr:format(Expr), string:titlecase(type(Expr))]
  ).

%% or you can do type(Expr) -> atom_to_list(hd(tuple_to_list(Expr))).
type({Type, _, _}) -> atom_to_list(Type);
type({Type, _, _, _}) -> atom_to_list(Type).

希望这会有所帮助:)