當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()]}。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。