qinHaiXiang
qinHaiXiang

Reputation: 6419

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

E.g: 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!!

Upvotes: 0

Views: 631

Answers (2)

xkeshav
xkeshav

Reputation: 54016

use this

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

Upvotes: 0

mario
mario

Reputation: 145472

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 A-Z 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.

Upvotes: 1

Related Questions