Erlang地图密钥模式匹配

发布于 2025-01-21 11:41:32 字数 135 浏览 2 评论 0原文

我对Erlang更新,我有一个像#{“ a/。+”:“ v1”,“ b/c/+”:“ v2”}的地图,

我想通过输入键获得值“匹配” A /.+”并获取“ V1”。

当键确切的时,并且输入密钥是正则是REGEX,我该如何实现它。

I am newer to erlang, I have a map like #{"a/.+":"v1", "b/c/.+": "v2"}

I want to get value by input key like "a/d" to match "a/.+" and get "v1".

It easy to pattern when key is exactly and input key is regex, how can I implement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2025-01-28 11:41:32

在这种情况下,您 必须使用地图。这个想法是使用Regex查找与输入的关键匹配,然后返回映射的值。
非优化版本正在使用re:run/2而无需编译模式。

L1 = [{"a/.+", "v1"}, {"b/c/.+", "v2"}],
LResult1 = lists:foldl(fun({K, V}, ListIn) ->  
            Match = re:run("a/d", K),
            case Match of
                nomatch -> ListIn;
                      _ -> ListIn ++ [{K, V}]
            end
        end,
        [],
        L1
        ),
case LResult1 of
    [] -> 
        no_matching_rule;
     _ -> 
        Hd1 = hd(LResult1),
        {K1, V1} = Hd1,
        V1
end.

优化的版本可以通过在使用之前对正则态度图案进行编译。

{ok, Mp1} = re:compile("a/.+"),
{ok, Mp2} = re:compile("b/c/.+"),
L2 = [{Mp1, "v1"}, {Mp2, "v2"}],
LResult2 = lists:foldl(fun({K, V}, ListIn) ->  
            Match = re:run("a/d", K),
            case Match of
                nomatch -> ListIn;
                      _ -> ListIn ++ [{K, V}]
            end
        end,
        [],
        L2
        ),
case LResult2 of
    [] -> 
        no_matching_rule;
     _ -> 
        Hd2 = hd(LResult2),
        {K2, V2} = Hd2,
        V2
end.

在Erlang Shell中粘贴上述任何代码都会产生“ V1”

上面的代码假定一个输入可以具有多个匹配的模式,但是只有第一个输入将作为输出打印。

For this case, you don't have to use map. The idea is to use regex to find which key match with the input, and then return the mapped value.
The un-optimized version is using re:run/2 without compiling the pattern.

L1 = [{"a/.+", "v1"}, {"b/c/.+", "v2"}],
LResult1 = lists:foldl(fun({K, V}, ListIn) ->  
            Match = re:run("a/d", K),
            case Match of
                nomatch -> ListIn;
                      _ -> ListIn ++ [{K, V}]
            end
        end,
        [],
        L1
        ),
case LResult1 of
    [] -> 
        no_matching_rule;
     _ -> 
        Hd1 = hd(LResult1),
        {K1, V1} = Hd1,
        V1
end.

The optimized version can be achieved by compiling the regex pattern once before using them.

{ok, Mp1} = re:compile("a/.+"),
{ok, Mp2} = re:compile("b/c/.+"),
L2 = [{Mp1, "v1"}, {Mp2, "v2"}],
LResult2 = lists:foldl(fun({K, V}, ListIn) ->  
            Match = re:run("a/d", K),
            case Match of
                nomatch -> ListIn;
                      _ -> ListIn ++ [{K, V}]
            end
        end,
        [],
        L2
        ),
case LResult2 of
    [] -> 
        no_matching_rule;
     _ -> 
        Hd2 = hd(LResult2),
        {K2, V2} = Hd2,
        V2
end.

Pasting any of above codes in Erlang shell will yield "v1".

Above code assumes that one input can have multiple matched pattern, but only the first one will be printed as output.

浅忆流年 2025-01-28 11:41:32

如果要在地图中找到与字符串匹配的所有键,则需要在整个地图上迭代:

a.erl:

-module(a).
-compile(export_all).

find(String, Map) ->
    MyMatchFun = fun (RegexKey, Val, Acc) -> 
                         case re:run(String, RegexKey) of
                             nomatch -> Acc;
                             {match, _} -> [Val | Acc]
                         end
                 end,
    maps:fold(MyMatchFun, _Acc=[], Map).

在shell中:

34> c(a).              
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

35> Map.
#{"a/.+" => "v1","a/b/.+" => "v3","b/c/.+" => "v2"}

36> a:find("a/d", Map).
["v1"]

36> a:find("b/c/dd", Map).
["v2"]

38> a:find("a/b/cc", Map).
["v3","v1"]

如果地图中只有一个键可以匹配您的字符串,则找到匹配项后,您可以在地图上停止迭代:

a.erl:

-module(a).
-compile(export_all).

find(String, Map) ->
    I = maps:iterator(Map),
    match_for(String, I).

match_for(String, I) ->
    case maps:next(I) of 
        none ->  %% then you have reached the end of the Map
            no_keys_in_map_matched_string;

        {RegExKey, Val, NewI} ->
            case re:run(String, RegExKey) of
                nomatch -> match_for(String, NewI);  %% continue iterating over the Map looking for a match
                {match, _} -> Val   %% found a match, so return the associated value
            end
    end.

在外壳中:

27> c(a).                
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

28> Map = #{"a/.+" => "v1", "b/c/.+" => "v2"}.

29> a:find("a/d", Map).                                         
"v1"

30> a:find("a/b/cc", Map).
"v1"

31> a:find("b/c/dd", Map).
"v2"

32> a:find("z", Map).     
no_keys_in_map_matched_string

If you want to find all the keys in the map that match your string, you will need to iterate over the whole map:

a.erl:

-module(a).
-compile(export_all).

find(String, Map) ->
    MyMatchFun = fun (RegexKey, Val, Acc) -> 
                         case re:run(String, RegexKey) of
                             nomatch -> Acc;
                             {match, _} -> [Val | Acc]
                         end
                 end,
    maps:fold(MyMatchFun, _Acc=[], Map).

In the shell:

34> c(a).              
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

35> Map.
#{"a/.+" => "v1","a/b/.+" => "v3","b/c/.+" => "v2"}

36> a:find("a/d", Map).
["v1"]

36> a:find("b/c/dd", Map).
["v2"]

38> a:find("a/b/cc", Map).
["v3","v1"]

If there is only one key in the map that will match your string, then you can stop iterating over the map as soon as you find a match:

a.erl:

-module(a).
-compile(export_all).

find(String, Map) ->
    I = maps:iterator(Map),
    match_for(String, I).

match_for(String, I) ->
    case maps:next(I) of 
        none ->  %% then you have reached the end of the Map
            no_keys_in_map_matched_string;

        {RegExKey, Val, NewI} ->
            case re:run(String, RegExKey) of
                nomatch -> match_for(String, NewI);  %% continue iterating over the Map looking for a match
                {match, _} -> Val   %% found a match, so return the associated value
            end
    end.

In the shell:

27> c(a).                
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

28> Map = #{"a/.+" => "v1", "b/c/.+" => "v2"}.

29> a:find("a/d", Map).                                         
"v1"

30> a:find("a/b/cc", Map).
"v1"

31> a:find("b/c/dd", Map).
"v2"

32> a:find("z", Map).     
no_keys_in_map_matched_string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文