1、使用正则表达式:
pattern = strcat('\b', strjoin(tuple, '\b|\b'), '\b');% 4. 逐行处理文件内容
modifiedContents = {}; % 存储修改后的内容
for i = 1:length(fileContents)line = fileContents{i};% 使用正则表达式检查当前行是否包含元组中的任何元素if ~isempty(regexp(line, pattern, 'once'))% 找到第一个 ',' 号并替换成 '*'commaPos = strfind(line, ',');if ~isempty(commaPos)line(commaPos(1)) = '*'; % 替换第一个 ',' 为 '*'endend% 将修改后的行添加到新的文件内容中modifiedContents{end+1} = line;
end
2、哈希表(containers.Map
)
tupleMap = containers.Map();
for i = 1:length(tuple)element = strtrim(tuple{i}); % 去除元组元素的前后空格tupleMap(element) = true; % 将元素存入 Map,值设为 true (值可以是任何的)
endif isKey(tupleMap, element) && contains(line, element)foundMatch = true;break;end
3、使用contain()与any的方式:
3、直接对所有的行使用正则表达式的方式