我怎样才能确保一个正则表达式中的一个匹配,一个不匹配?

时间:2021-08-19 04:57:35

This is simple, but I am taking an entire directory listing (in PHP with dir()), and making sure it both:

这很简单,但我正在整个目录列表(在PHP中使用dir()),并确保它们:

  • isn't "." or "..", and
  • 不是“。”或“......”,和

  • ends in a file extension .jpg, .jpeg, .gif, or .png.
  • 以文件扩展名.jpg,.jpeg,.gif或.png结尾。

Right now I have

现在我有

function isValidFile($filename) {
    $dirDotExpr = "/^\.{1,2}$/"; //matches against "." and ".."
    $extExpr = "/\.(jpg|jpeg|gif|png)$/i"; //checks file extension

    return (1
        && ! preg_match($dirDotExpr, $filename)
        && preg_match($extExpr, $filename)
    )
}

but it'd be nice to do it all in one regular expression. However, I don't know how to make sure one thing matches, and one thing doesn't--how can I accomplish this?

但是在一个正则表达式中完成所有操作会很好。但是,我不知道如何确保一件事情匹配,有一件事没有 - 我怎么能做到这一点?

3 个解决方案

#1


You may be overthinking it. If you're verifying that $filename ends in .jpg, .jpeg, .gif, or .png, it clearly can't be "." or "..".

你可能会过度思考它。如果您要验证$ filename以.jpg,.jpeg,.gif或.png结尾,则显然不能为“。”要么 ”..”。

#2


function isValidFile($filename)
{
    return (bool) preg_match('/\.(?:jpe?g|gif|png)$/iD', (string) $filename);
}

Note that I made the parentheses non-capturing, for a minor speed increase. Also merged jpg and jpeg into jpe?g. Also, I added the D modifier. If you don't, $ will allow final newlines; a filename like "hello.jpg\n" would be considered valid. Be very aware of that.

请注意,我将括号设为非捕获,以提高速度。还将jpg和jpeg合并为jpe?g。另外,我添加了D修饰符。如果不这样做,$将允许最终换行;像“hello.jpg \ n”这样的文件名将被视为有效。要非常清楚这一点。

#3


If you do not want to allow periods in your file name (except for the delimiter of the name and extension): ^[^\x2E]+\x2E(jpg|jpeg|gif|png)$

如果您不想在文件名中允许句点(名称和扩展名的分隔符除外):^ [^ \ x2E] + \ x2E(jpg | jpeg | gif | png)$

If you do allow for periods (multi-part name): ^([^\x2E]+\x2E)+(jpg|jpeg|gif|png)$

如果你允许句点(多部分名称):^([^ \ x2E] + \ x2E)+(jpg | jpeg | gif | png)$

Convert to PHP as required...

根据需要转换为PHP ...

#1


You may be overthinking it. If you're verifying that $filename ends in .jpg, .jpeg, .gif, or .png, it clearly can't be "." or "..".

你可能会过度思考它。如果您要验证$ filename以.jpg,.jpeg,.gif或.png结尾,则显然不能为“。”要么 ”..”。

#2


function isValidFile($filename)
{
    return (bool) preg_match('/\.(?:jpe?g|gif|png)$/iD', (string) $filename);
}

Note that I made the parentheses non-capturing, for a minor speed increase. Also merged jpg and jpeg into jpe?g. Also, I added the D modifier. If you don't, $ will allow final newlines; a filename like "hello.jpg\n" would be considered valid. Be very aware of that.

请注意,我将括号设为非捕获,以提高速度。还将jpg和jpeg合并为jpe?g。另外,我添加了D修饰符。如果不这样做,$将允许最终换行;像“hello.jpg \ n”这样的文件名将被视为有效。要非常清楚这一点。

#3


If you do not want to allow periods in your file name (except for the delimiter of the name and extension): ^[^\x2E]+\x2E(jpg|jpeg|gif|png)$

如果您不想在文件名中允许句点(名称和扩展名的分隔符除外):^ [^ \ x2E] + \ x2E(jpg | jpeg | gif | png)$

If you do allow for periods (multi-part name): ^([^\x2E]+\x2E)+(jpg|jpeg|gif|png)$

如果你允许句点(多部分名称):^([^ \ x2E] + \ x2E)+(jpg | jpeg | gif | png)$

Convert to PHP as required...

根据需要转换为PHP ...