[英]Regular Expression to find out special name pattern of folder's in PHP
例如:文件夾名稱的命名模式:
以一個字母開頭,以三個或十個數字結尾。
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;
}
那么,這是正確的方法嗎?
非常感謝你!!
您的方法幾乎是正確的。 但是您忘了^
,以便從一開始就進行比較:
preg_match('/^[A-Z][0-9]{1,9}$/i', $folders)
/i
僅在要區分大小寫的AZ時才需要。 如果要匹配1到10個數字,則{1,9}
應該變為{1,10}
。
請查看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.