Matlab regexp排除特定的文件扩展名集

时间:2020-04-11 20:52:22

标签: regex matlab

我要排除一组文件扩展名,否则要列出文件夹内容。

%get filenames in current directory
p = dir(pwd);
p = {p.name};
p = p(1:min(end,20))'
%construct regular expression
%exclude = {'ini','m'}; %just for your convenience
reg = '\.(^ini|m)$'; 

%actually print file names/paths of files without a certain extension
regexpi(p,reg,'match','once')

但是,这不起作用。如何获得排除这些文件扩展名的文件(路径中最后X个字符)?我尝试了[^abc],但这不包括不需要的单个字符。请在答案中使用regexp或regexprep

1 个答案:

答案 0 :(得分:0)

您写道:

reg = '\.(^ini|m)$';

^插入标记锚点位于.点之后, 因此它永远不会匹配字符串开头。 删除它FTW:

reg = '\.(ini|m)$'; 
相关问题