我试图在二郎一个解析器,在希望识别字符串中的数据类型。 经过搜索,我无法找到任何存在的问题是我的:
原始字符串:
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
有谁知道如何解决这个吗?
更新我的代码使用参数是“字符串,它是需要分析”,我上面所指出的:
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].