排除一个文件夹并使用regex匹配根文件夹中的所有.html模式文件


exclude a folder and match all .html pattern files in a root folder using regex

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

以下是html文件的格式

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

我只需要获得前两个html文件,即,我们正在排除以"_[0-9]'和"_ss[0-9]]"结尾的文件以及images文件夹中的.hmtl文件。

我已经成功地排除了3和4,但我无法排除images文件夹中的.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

有人能帮我解决这个问题吗。。

试试:

~^(?!/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