PHP regular expression in practice: matching file names

WBOY
Release: 2023-06-23 13:18:02
Original
2309 people have browsed it

PHP Regular Expression Practice: Matching File Names

Regular expression is a powerful text processing tool. PHP's own regular expression function provides many methods for pattern matching and replacement. During file processing, it is often necessary to filter and operate based on file names. This goal can be easily achieved using regular expressions.

  1. Match file name extension

First, we need to extract the extension in the file name. In PHP, you can use the pathinfo function to decompose a path into parts such as directories, file names, and extensions. However, if you only need to extract the extension part of the file name, you can use regular expressions to achieve this.

You can use the following regular expression to match the extension part of the file name:

preg_match('/.(?!.*/)([^./]+)$/', $filename, $matches);
Copy after login

Reverse lookahead is used in the regular expression, that is, any character that is not followed by a slash and a decimal point is matched. This ensures that only the part after the last decimal point is matched, and not decimal points in the file path. The matching results are stored in the $matches array, and the matching extension can be obtained through $matches[1].

For example, for a file named photo.jpg, use the above regular expression to extract jpg as the extension.

  1. Match specific file names

Next, we can use regular expressions to match specific file name formats. For example, if we want to match photo file names that start with a date followed by a 4-digit number, we can use the following regular expression:

preg_match('/^d{8}-d{4}.jpg$/', $filename, $matches);
Copy after login

Start and end anchors are used in the regular expression to ensure matching file names It must end with a combination of 8 digits, a dash, 4 digits, and .jpg. The matching results are stored in the $matches array. If no match is successful, $matches is an empty array.

For example, for a file named 20210101-1145.jpg, the above regular expression can be used to match successfully.

  1. Match multiple file name formats

Finally, we can use regular expressions to match multiple file names in different formats. For example, if we want to match file names that include .jpg, .png, and .gif extensions, we can use the following regular expression:

preg_match('/.(jpg|png|gif)$/', $filename, $matches);
Copy after login

The regular expression uses character sets and capturing groups to match file names starting with The part ending in .jpg, .png or .gif. The matching results are stored in the $matches array. If no match is successful, $matches is an empty array.

For example, for a file named photo.jpg, the above regular expression can be used to match successfully. Similarly, files named logo.png and banner.gif can also be matched successfully.

Conclusion

In file processing, regular expressions can help us easily match and manipulate file names. This article describes how to use regular expressions to match file name extensions, specific file name formats, and multiple file name formats, and provides corresponding sample code. By understanding and mastering these skills, you can improve the efficiency and accuracy of document processing.

The above is the detailed content of PHP regular expression in practice: matching file names. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!