[英]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.