正则应用之——日期正则表达式(Regular applications -- date
regular expressions)
1 Overview
First of all, it should be noted, either Winform, or Webform, has a very mature calendar controls, whether or scalability from the ease of use, selection and calibration date or use the calendar control to achieve better.
A few days ago in the CSDN section see the need for dates, regular posts, so tidy up this article, and discuss and exchange with you, if there are omissions or errors, but also please correct me.
The date regular is generally required for formatting, and the data is not used directly by the user input. Because of the different scenarios, the rules are different and the complexity is different. Regular writing needs to be analyzed in detail according to specific circumstances. A basic principle is to write only the right one and not the complex one.
For date extraction, as long as it can be separated from the non date, write the simplest regular, such as
\d{4}-\d{2}-\d{2}
If you can uniquely locate the date in the yyyy-MM-dd format in the source string, you can extract it.
For validation, it is not meaningful to verify only the
composition and format of the characters, but also to check the rules. Because of the existence of leap year, the check routine of dates becomes more complicated.
Let's examine the effective range of the date and what is a leap year.
2 date rule
2.1 the valid range of the date
For the valid range of the date, different application scenarios will vary.
The valid range of the DateTime object defined in MSDN is: 0001-01-01 00:00:00 to 9999-12-31 23:59:59.
UNIX timestamp 0, according to the ISO 8601 specification: 1970-01-01T00:00:00Z.
In practical applications, the date range will not go beyond the scope of DateTime, so regular verification takes the usual range of dates.
2.2 what is a leap year?
(excerpt from Baidu Encyclopedia)
Leap (year) is designed to make up for the time difference between the number of days per year for the calendar and the actual orbital period of the earth's revolution. The year that
makes up the time difference is a leap year.
The earth moves around the sun for 365 days, 5 hours, 48 minutes, 46 seconds (365.24219 days), that is, tropical year. The Gregorian calendar year only 365 days shorter than the tropical year of about 0.2422 days, every four years accumulated about a day, this day to the end of 2 (February 29th), so that the length of time for 366 days, this year is a leap year.
Note that now the Gregorian calendar is based on Roman "Julian" adapted to. Since we did not realize that we had to calculate more than 0.0078 days a year, the total was 10 days from 46 BC to sixteenth Century. Therefore, when the Pope Gregorian thirteen, October 5, 1582 to October 15th will be artificially specified. And began a new leap year provisions. Which provides the entire calendar year is a few hundred, must be a multiple of 400 is not a leap year, is a multiple of 400. For example, in 1700, 1800 and 1900 for the year 2000 is a leap year. Since then, the average annual length of 365.2425 days, about 4 years, a deviation of 1 days. According to a leap year every four years, an average of 0.0078 days per year will be calculated, and after four hundred years it will take about 3 days. Therefore, three leap yea
r will be reduced every four hundred years. The calculation of the leap year boils down to what it usually says: four years and one year; a hundred years without a leap, four hundred years later.
正则匹配年月日2.3 date format
Depending on the language and culture, the date's hyphen will vary, usually in the following formats:
YyyyMMdd
Yyyy-MM-dd
Yyyy/MM/dd
Yyyy.MM.dd
3 date regular expression construction
3.1 rule analysis
A commonly used method to write complex regular, is the first requirement of separation is not relevant, write the corresponding regular check, and then mix, the relationship and mutual influence, regular basically can draw the corres
ponding.
According to the definition of leap year, the date can be classified in several ways.
3.1.1 is divided into two categories depending on whether the number of days is related to the year
A class unrelated to the year and subdivided into two groups depending on the number of days per month
1, 3, 5, 7, 8, 10 and December were 1-31
4, 6, 9 and November were 1-30
Of or relating to a year
There is 1-28 on February?
Leap year, February, 1-29
3.1.2 can be divided into four categories depending on the date of inclusion
All months in all years contain 1-28 days
All years, except for February, contain 29 and 30 days
All 1, 3, 5, 7, 8, 10, and December all contain 31 days
Leap year February contains 29 days
3.1.3 classification method selection
Because the implementation of the date classification is achieved through the (exp1|exp2|exp3) branch structure,
The branch structure starts from the left branch to the right and tries to match. When a branch match succeeds, it attempts not to try right, otherwise, try all branches and report failure.
How many branches, the complexity of each branch will affect the matching efficiency, taking into account the validated date probability distribution, most of them fall within 1-28 days,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论