2016-11-24 1 views
1

私は、テキストファイル 'in'が与えられ、4文字すべてが '****'に置き換えられたファイルを返す関数を持っています。今まで私は各セルが1ワードである3d配列を得ることができました。 4文字の単語をすべて「****?」にするにはどうすればいいですか?Matlab 3次元配列で正規表現を使用するには?

%in.txt = 

    %word words 
    %words words words word 
    %words 
    %word word word 



fid = fopen(in); 

tline = fgetl(fid); 
string = {''}; 
while ischar(tline)%create an array where each cell is 1 line 
    string(length(string)+1) = {tline}; 
    tline = fgetl(fid); 
end 

str = {''}; 
for x=2:length(string)%create a matrix where each cell is is 1 line with each of those cells being 1 word 
    str(x) = {split(string(x), ' ')}; 
    end 
end 
+0

使用 'のregexprep(STR、 '\ <-ZA-Z] {4} \>'、 '****')' – obchardon

答えて

1

使用regexprep

fid = fopen('in.txt', 'r'); 

% (when running in a function) 
%OC = onCleanup(@() any(fopen('all')==fid) && fclose(fid)); 

data = regexprep(data{1}, ... 
       '\<(?:[-A-Za-z]{4})([^-A-Za-z]|$)',... 
       '****$1');    

fclose(fid); 
+0

はありますさよならのような言葉をそのまま残す方法? –

+0

@JakeDorinはい、更新情報をご覧ください –

関連する問題