Problems with strrep: y(1)0 instead of y(10)

3 次查看(过去 30 天)
I'm having a problem replacing some strings with strrep.
I have to replace some strings y1,y2,y3,y4 etc with y(1),y(2),y(3),y(4)and so on. This works fine until y(9), but for higher numbers it then makes for example for y10 = y(1)0 instead of y(10). What is the problem?
if true
%Create the strings for conversion table
table=cell(length(y_sym),2);
j = 1;
while j<=length(y_sym)
table{j,1}= strcat('y',num2str(j));
table{j,2}= strcat('y(',num2str(j),')');
j=j+1;
end
j=1;
while j<=length(y_sym)
str=strvcat(dy_sym(j));
if ~ischar(str)
str=char(str);
end
if any(size(str)~=1)
str=cellstr(str)
end
for i=1:size(table,1)
str_old = table{i,1};
str_new = table{i,2}
str=strrep(str,str_old,str_new)
end
if iscellstr(str)
fprintf(fid,repmat('%s\n',1,length(str)),str{:});
else
fprintf(fid,'%s\n',str);
end
j=j+1;
end
ok=true;
fclose(fid);
end
if nargout varargout{1}=ok; else if ok fprintf('Wrote %s successfully.\n',mfile_name); end
  2 个评论
Stephen23
Stephen23 2016-3-9
Marius's "Answer" moved here:
Thanks all it worked! Can someone tell me how i can write comments with fprintf? so that I have
%Comment here
in the file i create with fprintf?
Stephen23
Stephen23 2016-3-9
编辑:Stephen23 2016-3-9
Here are two ways:
>> fprintf('%s\n','% Comment here')
% Comment here
>> fprintf('%%%s\n','Comment here')
% Comment here
These are explained in the fprintf documentation.
PS: It is considered polite on this forum to accept the answer the helped resolve your question.

请先登录,再进行评论。

回答(3 个)

Stephen23
Stephen23 2016-3-8
编辑:Stephen23 2016-3-8
Instead of writing your own parser you should simply use regexprep:
>> str = 'abc y1 y2 mno y10 y11 xyz'
str =
abc y1 y2 mno y10 y11 xyz
>> regexprep(str,'y(\d+)','y($1)')
ans =
abc y(1) y(2) mno y(10) y(11) xyz
For learning about Regular Expressions you might like to try my FEX submission too:
  3 个评论
Stephen23
Stephen23 2016-3-8
编辑:Stephen23 2016-3-8
EDIT: Marius originally wrote a comment claiming that regeprep did not work. My response below.
Interesting... my answer shows that regexprep can do exactly what you want.
Please upload y_sym into a comment.
BTW, the problem is that you forgot to consider that y1 is a match before y10 is. My advice would be to use regexprep, which by default uses a greedy match and will always match the longer one correctly (when it is used properly). This is going to be much faster and more robust than trying to parse this yourself.
Stephen23
Stephen23 2016-3-8
@Marius: my pleasure! You can also accept my answer if it helped you.

请先登录,再进行评论。


Guillaume
Guillaume 2016-3-8
First, learn to use matlab's debugger. Had you stepped through the code, you'd have found the bug quickly.
You test the following search strings in order: 'y1', 'y2', ..., 'y10'. Therefore, in a string 'xxy10xx', your first iteration will find 'y1' and replace it with 'y(1)', and your string becomes 'xxy1(0)xx'. When it comes to searching 'y10', it's not to be found anymore. The fix is simple, search for the longer strings first, so reverse the order of your loop.
Better yet, no need for a loop or this building of search and replacement string, use a regular expression:
str = regexprep(str, 'y(\d+)', 'y\($1)')

Star Strider
Star Strider 2016-3-8
I would start by replacing ‘y10’ with ‘y(10)’ and move backwards to ‘y1’ replacing ‘y(1)’ last.

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by