octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Changeset] Re: Behavior of the matlab regexptranslate function


From: David Bateman
Subject: [Changeset] Re: Behavior of the matlab regexptranslate function
Date: Sun, 23 Mar 2008 23:22:34 +0100
User-agent: Thunderbird 2.0.0.12 (X11/20080306)

Ben Abbott wrote:
> I just finished the install of 2008a. The results from 2008a and 2007b
> (the same) are below.
> 
>>> regexp ('file1.mat file2.mat file3.xls file4.mat', ...
> regexptranslate('wildcard','*.mat'), 'match')
> 
> ans =
> 
>     'file1.mat file2.mat file3.xls file4.mat'
> 
>>> regexptranslate('wildcard','*.mat')
> 
> ans =
> 
> .*\.mat
> 
> Ben


Tine to file a bug report against matlab then.. In any case thinking
further about it replacing '*.mat' with '.*\.mat' makes as much sense as
replacing it with '\w+\.mat' as otherwise how do you treat filenames
with spaces in them.. I suspect this is a case of a matlab developer
trying the write the DoWhatIWantNotWhatIAsked function, and the moral is
that you should learn the syntax of regexp rather than relying on a
particular behavior of the regexptranslate function.

In any case I attach a changeset that duplicates the behavior of the
matlab function.

D.


# HG changeset patch
# User David Bateman <address@hidden>
# Date 1206310594 -3600
# Node ID 35e6d57df330e64c57c044e6fe22d6c316344751
# Parent  8584a758a95de0f34e22e01c10d83013a1528175
Add the regexptranslate function

diff --git a/scripts/strings/Makefile.in b/scripts/strings/Makefile.in
--- a/scripts/strings/Makefile.in
+++ b/scripts/strings/Makefile.in
@@ -35,7 +35,7 @@ INSTALL_DATA = @INSTALL_DATA@
 
 SOURCES = base2dec.m bin2dec.m blanks.m deblank.m dec2base.m \
   dec2bin.m dec2hex.m findstr.m hex2dec.m index.m isletter.m \
-  lower.m mat2str.m rindex.m split.m str2double.m str2mat.m \
+  lower.m mat2str.m regexptranslate.m rindex.m split.m str2double.m str2mat.m \
   str2num.m strcat.m cstrcat.m strcmpi.m strfind.m strjust.m strmatch.m \
   strncmpi.m strrep.m strtok.m strtrim.m strtrunc.m strvcat.m \
   substr.m upper.m
diff --git a/scripts/strings/regexptranslate.m 
b/scripts/strings/regexptranslate.m
new file mode 100644
--- /dev/null
+++ b/scripts/strings/regexptranslate.m
@@ -0,0 +1,48 @@
+## Copyright (C) 2008  David Bateman
+##
+## This file is part of Octave.
+##
+## Octave 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 3 of the License, or (at
+## your option) any later version.
+##
+## Octave 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.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} realsqrt (@var{x})
+## Return the real natural logarithm of @var{x}. If any element results in the
+## return value being complex @code{reallog} produces an error.
+## @seealso{log, realsqrt, realpow}
+## @end deftypefn
+
+function y = regexptranslate (op, x)
+  
+  if (ischar (op))
+    op = tolower (op);
+    if (strcmp ("wildcard", op))
+      y = regexprep (regexprep (regexprep (x, "\\.", "\\."), "\\*",
+                               ".*"), "\\?", ".");
+    elseif (strcmp ("escape", op))
+      ch = {'\$', '\.', '\?', '\[', '\]'};
+      y = x;
+      for i = 1 : length (ch)
+       y = regexprep (y, ch{i}, ch{i});
+      endfor
+    else
+      error ("regexptranslate: unexpected operation");
+    endif
+  else
+    error ("regexptranslate: expecting operation to be a string");
+  endif
+endfunction
+
+%!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.")
+%!assert (regexptranslate ("escape", '$.?[]'), '\$\.\?\[\]')

reply via email to

[Prev in Thread] Current Thread [Next in Thread]