regexp module with submatches available

Pascal Brisset <>
Mon Mar 26 15:48:03 CEST 2001


Robert Virding writes:
 > Having a call to just match and extract the groups would probably be
 > useful.  The question is whether to return the actual substrings or
 > return a list of start/length pairs like match does today.

Actually, gregexp:groups just maps lists:sublist/3 over a list of
start/length pairs returned by gregexp:re_apply/3.

Why not export re_apply/3 ? I'd rather use it than match/2, which
seems to iterate over the whole string to find the first match.

-- Pascal Brisset <> +33(0)141986741 --
-- Cellicium | 73 avenue Carnot | F-94230 Cachan +33(0)685110788 --


gregexp:groups(S, ParsedRegExp) ->
    case re_apply(S, 1, ParsedRegExp) of
       {match, _RestPos, _Rest, Groups} ->
           GetGroup = fun ({Start,Len}) -> lists:sublist(S,Start,Len) end,
           {match, lists:map(GetGroup, Groups)};
       Other -> Other
    end.




More information about the erlang-questions mailing list