Home  >  Article  >  How to parse filename formats such as "PUBLIC001" using regular expressions?

How to parse filename formats such as "PUBLIC001" using regular expressions?

WBOY
WBOYforward
2024-02-11 21:57:08962browse

In this article, php editor Zimo will introduce how to use regular expressions to parse file names named in formats such as "PUBLIC001". Regular expressions are a powerful text matching tool that can be used to extract required information quickly and accurately. For this specific file name format, we can extract the numeric part through appropriate regular expressions to achieve parsing purposes. Next, let's explore how to use regular expressions to parse this file name format!

Question content

Need help with regular expressions for parsing file names

The file will be named PUBLIC001 "PUBLIC" is static text in all filenames Last 3 digits - day of the year. 001 (Jan 1)-366 (December 31 of leap year) is the valid range

What is a regular expression. Is there a way to limit the maximum value to 366?

Thank you

Solution

Similar patterns:

public([0-2]\d\d|3[0-5]\d|36[0-6])

Will do.

Or, if verifying the date number is not important, something like:

public\d\d\d

or

public\d{3}

I will do the same.

If the file should not have any other parts, add ^ and $ at the beginning and end respectively (this will avoid matching not-public001 for example or public001002)

like:

^PUBLIC\d{3}$

The above is the detailed content of How to parse filename formats such as "PUBLIC001" using regular expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete