2011-12-08 12:39:58 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_regexp.erl
|
|
|
|
%%% Author : Badlop
|
|
|
|
%%% Purpose : Frontend to Re and Regexp OTP modules
|
|
|
|
%%% Created : 8 Dec 2011 by Badlop
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2011-12-08 12:39:58 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-12-08 12:39:58 +01:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_regexp).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
-compile([export_all]).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
exec({ReM, ReF, ReA}, {RgM, RgF, RgA}) ->
|
|
|
|
try apply(ReM, ReF, ReA) catch
|
|
|
|
error:undef -> apply(RgM, RgF, RgA);
|
|
|
|
A:B -> {error, {A, B}}
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run(binary(), binary()) -> match | nomatch | {error, any()}.
|
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
run(String, Regexp) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case exec({re, run, [String, Regexp, [{capture, none}]]},
|
|
|
|
{regexp, first_match, [binary_to_list(String),
|
|
|
|
binary_to_list(Regexp)]})
|
|
|
|
of
|
|
|
|
{match, _, _} -> match;
|
|
|
|
{match, _} -> match;
|
|
|
|
match -> match;
|
|
|
|
nomatch -> nomatch;
|
|
|
|
{error, Error} -> {error, Error}
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec split(binary(), binary()) -> [binary()].
|
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
split(String, Regexp) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case exec({re, split, [String, Regexp, [{return, binary}]]},
|
|
|
|
{regexp, split, [binary_to_list(String),
|
|
|
|
binary_to_list(Regexp)]})
|
|
|
|
of
|
|
|
|
{ok, FieldList} -> [iolist_to_binary(F) || F <- FieldList];
|
|
|
|
{error, Error} -> throw(Error);
|
|
|
|
A -> A
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec replace(binary(), binary(), binary()) -> binary().
|
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
replace(String, Regexp, New) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case exec({re, replace, [String, Regexp, New, [{return, binary}]]},
|
|
|
|
{regexp, sub, [binary_to_list(String),
|
|
|
|
binary_to_list(Regexp),
|
|
|
|
binary_to_list(New)]})
|
|
|
|
of
|
|
|
|
{ok, NewString, _RepCount} -> iolist_to_binary(NewString);
|
|
|
|
{error, Error} -> throw(Error);
|
|
|
|
A -> A
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec greplace(binary(), binary(), binary()) -> binary().
|
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
greplace(String, Regexp, New) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case exec({re, replace, [String, Regexp, New, [global, {return, binary}]]},
|
|
|
|
{regexp, sub, [binary_to_list(String),
|
|
|
|
binary_to_list(Regexp),
|
|
|
|
binary_to_list(New)]})
|
|
|
|
of
|
|
|
|
{ok, NewString, _RepCount} -> iolist_to_binary(NewString);
|
|
|
|
{error, Error} -> throw(Error);
|
|
|
|
A -> A
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec sh_to_awk(binary()) -> binary().
|
|
|
|
|
2011-12-08 12:39:58 +01:00
|
|
|
sh_to_awk(ShRegExp) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case exec({xmerl_regexp, sh_to_awk, [binary_to_list(ShRegExp)]},
|
|
|
|
{regexp, sh_to_awk, [binary_to_list(ShRegExp)]})
|
|
|
|
of
|
|
|
|
A -> iolist_to_binary(A)
|
2011-12-08 12:39:58 +01:00
|
|
|
end.
|