当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


erlang inspect(MP, Item)用法及代码示例


inspect(MP, Item) -> {namelist, [binary()]}
OTP 17.0
类型:
MP = mp()
Item = namelist

获取已编译的正则表达式和一个项目,并从正则表达式返回相关数据。唯一受支持的项目是 namelist ,它返回元组 {namelist, [binary()]} ,其中包含正则表达式中所有(唯一)命名子模式的名称。例如:

1> {ok,MP} = re:compile("(?<A>A)|(?<B>B)|(?<C>C)").
{ok,{re_pattern,3,0,0,
                <<69,82,67,80,119,0,0,0,0,0,0,0,1,0,0,0,255,255,255,255,
                  255,255,...>>}}
2> re:inspect(MP,namelist).
{namelist,[<<"A">>,<<"B">>,<<"C">>]}
3> {ok,MPD} = re:compile("(?<C>A)|(?<B>B)|(?<C>C)",[dupnames]).
{ok,{re_pattern,3,0,0,
                <<69,82,67,80,119,0,0,0,0,0,8,0,1,0,0,0,255,255,255,255,
                  255,255,...>>}}
4> re:inspect(MPD,namelist).                                   
{namelist,[<<"B">>,<<"C">>]}

请注意,在第二个示例中,重复的名称仅在返回的列表中出现一次,并且该列表按字母顺序排列,无论名称位于正则表达式中的位置。名称的顺序与捕获的子表达式的顺序相同,如果{capture, all_names}被指定为一个选项run/3。因此,您可以根据以下结果创建 name-to-value 映射run/3像这样:

1> {ok,MP} = re:compile("(?<A>A)|(?<B>B)|(?<C>C)").
{ok,{re_pattern,3,0,0,
                <<69,82,67,80,119,0,0,0,0,0,0,0,1,0,0,0,255,255,255,255,
                  255,255,...>>}}
2> {namelist, N} = re:inspect(MP,namelist).
{namelist,[<<"A">>,<<"B">>,<<"C">>]}
3> {match,L} = re:run("AA",MP,[{capture,all_names,binary}]).
{match,[<<"A">>,<<>>,<<>>]}
4> NameMap = lists:zip(N,L).
[{<<"A">>,<<"A">>},{<<"B">>,<<>>},{<<"C">>,<<>>}]

相关用法


注:本文由纯净天空筛选整理自erlang.org大神的英文原创作品 inspect(MP, Item) -> {namelist, [binary()]}。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。