MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   【求助】文件内字符串查找和替换的问题 (https://www.labfans.com/bbs/showthread.php?t=980)

efoxxx 2007-12-21 14:02

【求助】文件内字符串查找和替换的问题
 
我用这段代码替换文件中的某些字符串的出现一个问题:替换后原文件中 “%”以及“%”后面(同一行的)信息丢掉了,估计是%在matlab中被当作注释处理掉了,请问有什么办法解决没有?多谢了

function findreplace(file,otext,ntext,varargin)
%FINDREPLACE finds and replaces strings in a text file
%
% SYNTAX:
%
% findreplace(file,otext,ntext)
% findreplace(file,otext,ntext,match)
%
% findreplace : This function finds and replaces strings in a text file
%
% file: text file name (with or without path)
% otext: text to be replaced (old text)
% ntext: replacing text (new text)
% match: either (1) for match case or (0) to ignore case
% default value is (1)
%
% Example:
% findreplace('sample.txt','Moller','Moler');
% findreplace('sample.txt','jake','Jack',0);
% findreplace('sample.txt','continue it is','continue its',0);
%
% Copyright 2005 Fahad Al Mahmood
% Version: 1.0 $Date: 24-Dec-2005

% Obtaining the file full path
[fpath,fname,fext] = fileparts(file);
if isempty(fpath)
out_path = pwd;
elseif fpath(1)=='.'
out_path = [pwd filesep fpath];
else
out_path = fpath;
end

% Reading the file contents
k=1;
all=1; % replace all (Nono's comment.) It should be 0 for interative prompt.
opt=[];
first_time=1;
change_counter=0;
fid = fopen([out_path filesep fname fext],'r');
while 1
line{k} = fgetl(fid);
if ~ischar(line{k})
break;
end
k=k+1;
end
fclose(fid);
old_lines = line;

%Number of lines
nlines = length(line)-1;

for i=1:nlines
if nargin==3, match=1;
else match=varargin{1}; end

if match==1, loc = regexp(line{i},otext);
elseif match==0, loc = regexpi(line{i},otext);
end

if ~isempty(loc)
nloc = 1;
for j=1:length(loc)
if all==0
% Displaying keyboard instructions
if first_time
disp(' ');
disp('(y) change (n) skip (a) change all (s) stop');
disp(' ');
first_time=0;
end
disp(line{i});
opt = input(underline(loc(j),length(otext)),'s');
if opt=='a'
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
all=1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
elseif opt=='y'
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
elseif opt=='s';
break;
else
nloc = nloc + 1;
end
else
line{i} = regexprep(line{i},otext,ntext, 'preservecase',nloc);
change_counter = change_counter + 1;
if length(loc)>j
loc(j:end) = loc(j:end) + (length(ntext)-length(otext));
end
end
end
end
if opt=='s';
break
end
end

line = line(1:end-1);

disp(' ');
if change_counter~=0
% Writing to file
fid2 = fopen([out_path filesep fname fext],'w');

for i=1:nlines
fprintf(fid2,[line{i} '\n']);
end
fclose(fid2);
disp([num2str(change_counter) ' Changes Made & Saved Successfully']);
disp(' ');
else
disp('No Match Found / No Change Applied');
disp(' ');
end

function uline = underline(loc,length)
s=' ';
l='-';
uline=[];
if loc==1
for i=1:length
uline=[uline l];
end
else
for i=1:loc-1
uline = [uline s];
end
for i=1:length
uline=[uline l];
end
end
uline = [uline s];

elunxu 2007-12-21 18:32

没有出现楼主所说的问题。

x.txt
function findreplace(file,otext,ntext,varargin)
%FINDREPLACE finds and replaces strings in a text file
%
% SYNTAX:
%
% findreplace(file,otext,ntext)
% findreplace(file,otext,ntext,match)
%
% findreplace : This function finds and replaces strings in a text file
%
% file: text file name (with or without path)
% otext: text to be replaced (old text)
% ntext: replacing text (new text)
% match: either (1) for match case or (0) to ignore case
% default value is (1)
%
>> findreplace('D:\x.txt','%','a')

15 Changes Made & Saved Successfully

处理得到的结果:
function findreplace(file,otext,ntext,varargin)
aFINDREPLACE finds and replaces strings in a text file
a
a SYNTAX:
a
a findreplace(file,otext,ntext)
a findreplace(file,otext,ntext,match)
a
a findreplace : This function finds and replaces strings in a text file
a
a file: text file name (with or without path)
a otext: text to be replaced (old text)
a ntext: replacing text (new text)
a match: either (1) for match case or (0) to ignore case
a default value is (1)
a

efoxxx 2007-12-22 01:43

版主试试这句:
findreplace('aaaa.txt','text','test')

结果只剩下第一行了

gunzhusigan 2012-07-12 17:18

回复: 【求助】文件内字符串查找和替换的问题
 
英文来的看不懂!





















SIGNATURE............................................................................................................................................................
[url=http://www.gunzhusigan.com/]丝杆[/url]|[url]http://www.gunzhusigan.com/[/url]


所有时间均为北京时间。现在的时间是 13:11

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.