简体   繁体   中英

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. The {1,9} should become {1,10} if you want to match 1 to 10 numbers.

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.

用这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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