Home > functions > internal > dsStrrep.m

dsStrrep

PURPOSE ^

STRREP - replace full words by new character strings, ignoring matches that appear as sub-strings.

SYNOPSIS ^

function str = dsStrrep(str,oldstr,newstr,lpad,rpad, varargin)

DESCRIPTION ^

STRREP - replace full words by new character strings, ignoring matches that appear as sub-strings.

 Note: built-in strrep replaces ALL matches.

 Examples:
   dsStrrep('(v)*(-av)','v','pop1_v')
   dsStrrep('v-v^2+vav','v','pop1_v')
   dsStrrep('v-v-v','v','pop1_v')
   dsStrrep('v-v-v^2','v','pop1_v')
   dsStrrep('(v-v-v^2)','v','pop1_v')
   dsStrrep('E-pop1_V+1','pop1_V','pop1_V(n-1)')
   dsStrrep('v=1; u=u+d','u','u(n,test)')

   'new.old' == dynasim_strrep('old.old','old','new')
   'new.old.old' == dynasim_strrep('old.old.old','old','new')
 
 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function str = dsStrrep(str,oldstr,newstr,lpad,rpad, varargin)
0002 %STRREP - replace full words by new character strings, ignoring matches that appear as sub-strings.
0003 %
0004 % Note: built-in strrep replaces ALL matches.
0005 %
0006 % Examples:
0007 %   dsStrrep('(v)*(-av)','v','pop1_v')
0008 %   dsStrrep('v-v^2+vav','v','pop1_v')
0009 %   dsStrrep('v-v-v','v','pop1_v')
0010 %   dsStrrep('v-v-v^2','v','pop1_v')
0011 %   dsStrrep('(v-v-v^2)','v','pop1_v')
0012 %   dsStrrep('E-pop1_V+1','pop1_V','pop1_V(n-1)')
0013 %   dsStrrep('v=1; u=u+d','u','u(n,test)')
0014 %
0015 %   'new.old' == dynasim_strrep('old.old','old','new')
0016 %   'new.old.old' == dynasim_strrep('old.old.old','old','new')
0017 %
0018 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0019 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0020 
0021 if nargin<4, lpad=''; end
0022 if nargin<5, rpad=''; end
0023 if nargin > 5
0024   if ~ischar(lpad) || ~ischar(rpad)
0025     varargin = [lpad, rpad, varargin];
0026     lpad='';
0027     rpad='';
0028   end
0029 end
0030 if isempty(str)
0031   return;
0032 end
0033 
0034 %% auto_gen_test_data_flag argin
0035 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0036 if options.auto_gen_test_data_flag
0037   varargs = varargin;
0038   varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0039   varargs(end+1:end+2) = {'unit_test_flag',1};
0040   argin = [{str}, {oldstr}, {newstr}, {lpad}, {rpad}, varargs]; % specific to this function
0041 end
0042 
0043 pat=['([^\w\.]{1})' oldstr '([^\w]{1})']; % in the middle
0044   % NOTE: exclude .oldstr for case where prefix has already been prepended and oldstr appears >1x in str
0045 rep=['$1' lpad newstr rpad '$2'];
0046 str=regexprep(str,pat,rep);
0047 
0048 % check for neighboring occurrence that wasn't substituted (e.g., (v-v^2) -> (pop1_v-v^2))
0049 % NOTE: this is only a possible issue for strings "in the middle"
0050 test=['([^\w\.]{1})' oldstr '([^\w(' newstr ')]{1})'];
0051 test = strrep(test,'-','\-');
0052 if ~isempty(regexp(str,test,'once'))%~isempty(regexp(str,test,'match'))
0053   % substitute remaining occurrences
0054   str=regexprep(str,test,rep);
0055 end
0056 
0057 pat=['([^\w\.]{1})' oldstr '$'];    % at the end
0058 rep=['$1' lpad newstr rpad];
0059 str=regexprep(str,pat,rep);
0060 pat=['^' oldstr '([^\w]{1})'];      % at the beginning
0061 rep=[lpad newstr rpad '$1'];
0062 str=regexprep(str,pat,rep);
0063 pat=['^' oldstr '$'];               % all there is
0064 rep=[lpad newstr rpad];
0065 str=regexprep(str,pat,rep);
0066 
0067 %% auto_gen_test_data_flag argout
0068 if options.auto_gen_test_data_flag
0069   argout = {str}; % specific to this function
0070   
0071   dsUnitSaveAutoGenTestData(argin, argout);
0072 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005