Home > functions > internal > dsStrrep2.m

dsStrrep2

PURPOSE ^

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

SYNOPSIS ^

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

DESCRIPTION ^

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

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

   'new.new' == dsStrrep2('old.old','old','new')
   'new.new.new' == dsStrrep2('old.old.old','old','new')

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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

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