0
$fileNameMatchRegex = ^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$

$fileNames = 

Array  
(
    [0] => index.html
    [1] => a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv
    [2] => a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv
    [3] => a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv
    [4] => a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv
    [5] => a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv
    [6] => a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv
    [7] => a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv
    [8] => a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv
    [9] => a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv
)

$matchingFileNames  = preg_grep ("/".$fileNameMatchRegex."/", $fileNames);

这在这里有效...我在 1-9 上得到了匹配:正则表达式测试器

4

1 回答 1

2

它对我来说工作正常

    $fileNames = explode(',', "index.html,a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv,a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv,a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv,a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv,a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv,a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv,a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv,a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv,a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv");
    $fileNameMatchRegex = '^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$';
    $matchingFileNames  = preg_grep ("#".$fileNameMatchRegex."#", $fileNames);

    print_r($fileNames);
    print_r($matchingFileNames);

但是我在正则表达式周围使用了“#”作为分隔符,因为您的正则表达式包含“/”,所以您只需要转义您的“/”或选择更好的分隔符

于 2013-08-12T23:17:45.870 回答