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

时间:2011-03-15 05:51:37

标签: php regex

例如:文件夹名称的命名模式:

以一个字母开头,以三个或十个数字结尾。

F3445656
A543545
F454534535

我的正则表达式是:

$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;
    }

那么,这是正确的方法吗?!

非常感谢!!

2 个答案:

答案 0 :(得分:1)

你的方法几乎是正确的。但是你忘了^从头开始比较它:

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

只有在您想要不区分大小写的情况下匹配A-Z时才需要/i。如果您想匹配1到10个数字,{1,9}应该变为{1,10}

请查看https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world以获取一些可以帮助设计正则表达式的好工具。

答案 1 :(得分:0)

使用此

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