如何用'*'替换文本文件中的某些单词?

时间:2015-03-05 19:14:44

标签: string matlab io

您好我有一个

的文本文件
  Treadstone project

  Jason Bourne is to neutralize Wombosi.

  Nicky Parsons is the technician on the job

  Bourne has shown interest in Marie Kreutz

  Do not leak information about Blackbriar.

我有一个

的单元格数组
words = {'treadstone','bourne','wombosi','parsons','blackbriar'}

我想要得到这个

********** project

Jason ****** is to neutralize *******.

Nicky ******* is the technician on the job

****** has shown interest in Marie Kreutz

Do not leak information about **********.

但我的代码正在打印

********** project

Jason ****** is to neutralize Wombosi

Jason bourne is to neutralize *******.

Nicky ******* is the technician on the job

****** has shown interest in Marie Kreutz

Do not leak information about **********.

这是我的代码:

while ischar(line)
if strcmp(line, '')
    fprintf(output, line);
    line = fgetl(fh);
end
[T N] = size(words);
for i = 1:N
    mat = words{i}; %extreact the first word to comapre it to the text file
    if strfind(lower(line), mat)
        t = mat; 
        t(1:end) = '*'; %replace the word with *
        ht = strfind(lower(line), mat); %find its location 
        hat = lower(line(ht));
        line(ht) = hat; %replace the word with lower case
        lalu = strrep(line, mat, t); 

        fprintf(output, '%s\n', lalu);
    else 
        hat = 0;
    end


end

line = fgetl(fh);
end

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您的代码大部分都有效。但是,如果一个句子中有多个匹配的字符串,则不会正确更新该句子以阻止每个单词。您只是单独阻止单词,而不是更新一个字符串中的所有单词。完成后,您还需要将fprintf语句放在内部循环之外。通过将fprintf语句放在for循环中,您可以在查找句子中的特定字符串成功时编写该字符串。一旦检查所有单词,您只想将字符串写入文本 - 这就是您的文本文件重复某些句子的原因。这是因为句子中有多个匹配的字符串。

因此,做这样的事情:

while ischar(line)
if strcmp(line, '')
    fprintf(output, line);
    line = fgetl(fh);
end
[T N] = size(words);
for i = 1:N
    mat = lower(words{i}); %extreact the first word to comapre it to the text file
                           % Also cast to lower to ensure case-sensitive
    if strfind(lower(line), mat)
        t = mat; 
        t(1:end) = '*'; %replace the word with *
        ht = strfind(lower(line), mat); %find its location 
        hat = lower(line(ht));
        line(ht) = hat; %replace the word with lower case
        line = strrep(line, mat, t);   %// UPDATE STRING HERE
    else 
        hat = 0;
    end           
end

fprintf(output, '%s\n', line); %// Print out string once all replacing is done

line = fgetl(fh);
end

在让代码运行之前,我必须做一些设置。我将您的文本放在名为bourne.txt的文件中,然后在运行代码之前执行此操作:

fh = fopen('bourne.txt', 'r');
line = fgetl(fh);
words = {'treadstone','bourne','wombosi','parsons','blackbriar'};
output = fopen('output.txt', 'w');

我把文字写到output.txt。当我使用上面的代码并运行更正后的代码时,我得到了:

 ********** project
 Jason ****** is to neutralize *******.
 Nicky ******* is the technician on the job
 ****** has shown interest in Marie Kreutz
 Do not leak information about **********.

现在我们已成功将您的文本文件归类,Jason Bourne将感到自豪: