Reputation: 6419
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
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