2
%% <year>2003-2008</year>
3
%% <holder>Ericsson AB, All Rights Reserved</holder>
6
%% ``The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
17
%% The Initial Developer of the Original Code is Ericsson AB.
22
-export([match/2, lmatch/2, match_pos/2]).
25
-import(lists, [reverse/1]).
27
-include("ct_util.hrl").
30
%% @doc For debugging only.
32
tmatch("glurf", "glarf"),
33
tmatch("kalle sover", "([^ ]*) so").
35
tmatch(Str, Pattern) ->
36
io:format("~s =~~ /~s/", [Str,Pattern]),
37
io:format("=> ~p\n", [match(Str, Pattern)]).
40
DriverPath = code:priv_dir(common_test) ++ "/lib",
41
erl_ddll:load_driver(DriverPath, "erl_rx_driver"),
42
P = open_port({spawn,erl_rx_driver}, []),
43
register(erl_rx_driver, P),
47
case whereis(erl_rx_driver) of
49
init(); % a raise condition might be possible
53
port_control(erl_rx_driver, 0, Data).
56
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58
%% @spec match(Str, RegExp) -> nomatch | [string()]
59
%% Str = string() | [string|binary] | binary
60
%% RegExp = string() | binary
62
%% @doc This function tries to match the Posix regular expression
63
%% <code>RegExp</code> with the contents of <code>Str</code>. It returns
64
%% a list of matched strings if a match was found and
65
%% <code>nomatch</code> otherwise. The list of matched strings looks
66
%% like this: <code>[FullMatch, SubMatch1, SubMatch2, ...]</code>
67
%% where <code>FullMatch</code> is the string matched by the whole
68
%% regular expression and <code>SubMatchN</code> is the string that
69
%% matched subexpression no N. Subexpressions are denoted with '(' ')'
70
%% in the regular expression
74
%% match("abc01xyz02rst23", "abc[0-9][0-9]"),
77
%% match("abc01xyz02rst23", "([a-z]+[0-9]+)([a-z]+[0-9]+)([a-z]+[0-9]+)"),
78
%% returns ["abc01xyz02rst23","abc01","xyz02","rst23"]
84
match(Str, RegExp, strings).
87
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89
%% @spec match_pos(Str, RegExp) -> nomatch | [{Start,End}]
93
%% @doc This function is equivalent to <code>match/2</code>, but it
94
%% returns a list of positions instead for a list of strings.
95
match_pos(Str, RegExp) ->
96
match(Str, RegExp, pos).
100
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
%% @spec lmatch(Lines, RegExp) -> nomatch | {[string()],Rest}
105
%% Str = string() | [string|binary] | binary
106
%% RegExp = string() | binary
108
%% @doc Performs match/2 on each string in <code>Lines</code>. The
109
%% first match found is returned along with the rest of the
110
%% <code>Lines</code>. If no match is found, <code>nomatch</code> is
116
lmatch([Str|RestLines], RegExp) ->
117
case match(Str, RegExp) of
119
lmatch(RestLines, RegExp);
128
%%%-----------------------------------------------------------------
129
%%% Internal Functions
130
match(Str, RegExp, ReturnType) when is_binary(RegExp) ->
131
Data = [<<(size(RegExp)):32/native,RegExp/binary>>|Str],
132
return(ReturnType, list_to_binary(Str),port_call(Data));
133
match(Str, RegExp, ReturnType) when is_list(RegExp) ->
134
match(Str, list_to_binary(RegExp), ReturnType).
137
return(_Type,_,[]) -> nomatch;
138
return(Type, Str, Bin) ->
139
return_1(Type, Str, Bin, []).
141
return_1(Type, Str, <<S:32/native,E:32/native,T/binary>>, Acc) ->
142
Result = return_2(Type,Str,E,S),
143
return_1(Type, Str, T, [Result|Acc]);
144
return_1(_, _, <<>>, Acc) -> reverse(Acc).
147
return_2(strings, Str, E, S) ->
149
<<_:S/binary,Result:ResLen/binary,_/binary>> = Str,
150
binary_to_list(Result);
151
return_2(pos, _Str, E, S) ->