I need a regular expression that will detect a filename from a string - see if it ends with .pdf, or .PDF - case insensitive. Should even match .Pdf or any variant of pdf in case the user has messy filenames.
This will be used in PHP 5. I know I can make a bunch of rows to test against each case, but I'm sure there's a more elegant way to do this.
正则表达式没有任何问题,但是还有一个现成的函数用于剖析路径并从中提取扩展:
echo pathinfo("/path/to/myfile.txt", PATHINFO_EXTENSION); //.txt
how about this one. I dont know what language you are using but here is a regex for matching anything ending in .pdf
.+([.][Pp][Dd][Ff]){1}
my bad. Im half asleep. PHP it is. dont know php but that regex should work
另一种可能性是延长
strtolower(pathinfo("/path/file.pDf", PATHINFO_EXTENSION)) == ".pdf"
正如其他人所说,提取扩展名会有效,否则你可以做这样的事情。
preg_match('/.*\\.pdf/i', "match_me.pDf", $matches);
If your string consists of a single filename here is a simple regex solution:
if (preg_match('/\.pdf$/i', $filename)) {
// Its a PDF file
}
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.