diff --git a/common/utils.erl b/common/utils.erl index e880324..51a0a76 100644 --- a/common/utils.erl +++ b/common/utils.erl @@ -10,16 +10,7 @@ try_parse_number(String) -> is_digit(Char) -> lists:member(Char, lists:seq($0, $9)). -re_escape(Symbol) -> - case lists:member(Symbol, "[]\\^$.|?*+()") of - true -> - case is_integer(Symbol) of - true -> [$\ | [Symbol]]; - false -> [$\ | Symbol] - end; - false -> - case is_integer(Symbol) of - true -> [Symbol]; - false -> Symbol - end - end. \ No newline at end of file +re_escape(String) -> + io:fwrite("Token:~p, is binary: ~p~n", [String, is_binary(String)]), + Escaped = re:replace(String, "[\\[\\]\\\\^$.|?*+()]", "\\\\&", [global]), + Escaped. \ No newline at end of file diff --git a/reverse_polish_notation_evaluator/src/reverse_polish_notation_evaluator.erl b/reverse_polish_notation_evaluator/src/reverse_polish_notation_evaluator.erl index 334cc86..e56b2b5 100644 --- a/reverse_polish_notation_evaluator/src/reverse_polish_notation_evaluator.erl +++ b/reverse_polish_notation_evaluator/src/reverse_polish_notation_evaluator.erl @@ -31,8 +31,18 @@ handle_token(Token, Stack, Position) -> tokenize(Expression) -> - Tokens = re:split(Expression, "(\\d+\\.?\\d*|[-+*/%//**])"), - lists:filter(fun(X) -> X =/= "" end, Tokens). + Tokens = re:split(Expression, "\\s+"), + FilteredTokens = lists:filter(fun(X) -> (X =/= "") and not(lists:member(X, "\s\t\n")) end, Tokens), + io:fwrite("~p~n", [FilteredTokens]), + Positions = lists:flatmap(fun(Token) -> + case re:run(Expression, utils:re_escape(Token), [global]) of + nomatch -> []; + {match, Captured} -> + io:fwrite("~p:~p~n", [utils:re_escape(Token), Captured]), + lists:map(fun({Index, _Length}) -> {Token, Index + 1} end, lists:flatten(Captured)) + end + end, FilteredTokens), + Positions. @@ -41,4 +51,4 @@ tokenize(Expression) -> start() -> - io:fwrite("~p~n", [tokenize(1 2 3 * + 4 -)]). + io:fwrite("~p~n", [tokenize("-11.4 2 3 / + * // ** % + 4 -")]).