简体   繁体   English

正则表达式以找出PHP中文件夹的特殊名称模式

[英]Regular Expression to find out special name pattern of folder's in PHP

Eg: The named pattern of folder's name : 例如:文件夹名称的命名模式:

Begin with one letter and end with three or ten number. 以一个字母开头,以三个或十个数字结尾。

F3445656
A543545
F454534535

And my regular expression is : 我的正则表达式是:

$path = realpath('k:\\folder\\');

$folders = new DirectoryIterator($path);
foreach($folders as $folder){
    if($folder->isDot()) continue;
    if ($folder->isDir() && preg_match('/[A-Z][0-9]{1,9}$/', $folders)){
        echo $folder;
    }

So, is it the right way to do?! 那么,这是正确的方法吗?

Thank you very much!! 非常感谢你!!

Your approach was almost correct. 您的方法几乎是正确的。 But you forgot ^ to make it compare from the start: 但是您忘了^ ,以便从一开始就进行比较:

preg_match('/^[A-Z][0-9]{1,9}$/i', $folders)

The /i is only necessary if you want to match AZ case-insensitively. /i仅在要区分大小写的AZ时才需要。 The {1,9} should become {1,10} if you want to match 1 to 10 numbers. 如果要匹配1到10个数字,则{1,9}应该变为{1,10}

Please check out https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world for some nice tools that can assist in designing regular expressions. 请查看https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world中的一些不错的工具,这些工具可以帮助设计正则表达式。

用这个

preg_match('/^[A-Z]{1}[0-9]{3,9}$/i', $folders))

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

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