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.
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);
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.
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);
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.
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);
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!