使用正则表达式排除文件夹并匹配根文件夹中的所有.html模式文件

时间:2013-11-26 09:11:05

标签: php regex

我需要编写一个正则表达式,它匹配除图像文件夹以外的所有文件夹中的所有.html文件。

以下是html文件的格式

/magazines/sample.html 
/test/index.html
/test/format_ss1.html
/test/folder/newstyle_1.html
/images/two.html

我只需要获得前2个html文件,即我们要排除以图像文件夹中的“_ [0-9]”和“_ss [0-9]”以及.hmtl文件结尾的文件。

我已成功完成排除3和4但我无法在图像文件夹中排除.html文件。

$regex = '/[a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; //this will do for 3 and 4 files 

但我需要排除图片文件夹..

我试过像

$regex = '/[^images\/][a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; // not working

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

尝试使用:

~^(?!/images/)[a-zA-Z0-9/-]+(?!_ss\d|\d)\.html$~ 

正在行动:

$arr = array(
'/magazines/sample.html',
'/test/index.html',
'/test/format_ss1.html',
'/test/folder/newstyle_1.html',
'/images/two.html'
);
foreach($arr as $str) {
    if (preg_match('~^(?!/images/)[a-zA-Z0-9/-]+(?!_ss\d|\d)\.html$~', $str))
        echo $str,"\n";
}

<强>输出:

/magazines/sample.html
/test/index.html