1######################################################################## 2## 3## Copyright (C) 2008-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} regexptranslate (@var{op}, @var{s}) 28## Translate a string for use in a regular expression. 29## 30## This may include either wildcard replacement or special character escaping. 31## 32## The behavior is controlled by @var{op} which can take the following 33## values 34## 35## @table @asis 36## @item @qcode{"wildcard"} 37## The wildcard characters @code{.}, @code{*}, and @code{?} are replaced with 38## wildcards that are appropriate for a regular expression. For example: 39## 40## @example 41## @group 42## regexptranslate ("wildcard", "*.m") 43## @result{} '.*\.m' 44## @end group 45## @end example 46## 47## @item @qcode{"escape"} 48## The characters @code{$.?[]}, that have special meaning for regular 49## expressions are escaped so that they are treated literally. For example: 50## 51## @example 52## @group 53## regexptranslate ("escape", "12.5") 54## @result{} '12\.5' 55## @end group 56## @end example 57## 58## @end table 59## @seealso{regexp, regexpi, regexprep} 60## @end deftypefn 61 62function y = regexptranslate (op, s) 63 64 if (nargin != 2) 65 print_usage (); 66 endif 67 68 if (! ischar (op)) 69 error ("regexptranslate: operation OP must be a string"); 70 endif 71 72 op = tolower (op); 73 if (strcmp ("wildcard", op)) 74 y = strrep (strrep (strrep (s, '.', '\.'), 75 '*', '.*'), 76 '?', '.'); 77 elseif (strcmp ("escape", op)) 78 y = regexprep (s, '([][(){}.*+?^$|\\])', '\\$1'); 79 else 80 error ("regexptranslate: invalid operation OP"); 81 endif 82 83endfunction 84 85 86%!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.") 87%!assert (regexptranslate ("escape", '^.?[abc]$'), '\^\.\?\[abc\]\$') 88 89## Test input validation 90%!error <Invalid call to regexptranslate> regexptranslate () 91%!error <Invalid call to regexptranslate> regexptranslate ("wildcard") 92%!error <Invalid call to regexptranslate> regexptranslate ("a", "b", "c") 93%!error <invalid operation> regexptranslate ("foo", "abc") 94%!error <operation OP must be a string> regexptranslate (10, "abc") 95