简体   繁体   English

正则表达式文件名匹配

[英]Regular expression filename matching

I am writing a PHP function in Drupal to detect duplicate file uploads and attempting to compare the uploaded filename to previously uploaded files. 我在Drupal中编写PHP函数,以检测重复的文件上传,并尝试将上传的文件名与以前上传的文件进行比较。

I have example files of: 我有以下示例文件:

trees-nature_0.jpg
trees-nature_1.jpg
trees-nature0.jpg
trees-nature.jpg

I am trying to match all of them all using the following code: 我正在尝试使用以下代码来匹配所有这些对象:

file_scan_directory('image/uploads', "/trees-nature[*]?.jpg/");

However, all I get back is trees-nature.jpg . 但是,我得到的只是trees-nature.jpg

I would appreciate some correction. 我希望得到一些纠正。

Your regex is not correct. 您的正则表达式不正确。 use: 采用:

file_scan_directory('image/uploads', '/trees-nature.*?\.jpg/');

You can use the following: 您可以使用以下内容:

file_scan_directory('image/uploads', '/trees-nature(.*?)\.jpg/');

Correction: 更正:

  • [ ] cannot be used as parentheses.. it has special meaning in regex [ ]不能用作括号..在正则表达式中具有特殊含义
  • * is not wildcard in regex.. you have to use .* *在正则表达式中不是通配符。您必须使用.*
  • . also has special meaning here (any character) you need to escape it 这里也有特殊含义(任何字符),您需要对其进行转义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM