In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of a sample analysis of regular expressions on dates. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1 Overview
First of all, it needs to be noted that both Winform and Webform have very mature calendar controls. No matter from the point of view of ease of use or scalability, it is better to use calendar controls to select and verify dates.
A few days ago, I saw posts that need regular dates in several sections of CSDN, so I sorted out this article and discussed it with you. If there are any omissions or mistakes, please correct them.
Date regularization is generally used when formatting is required and the data is not entered directly by the user. Due to different application scenarios, the rules written are also different, and the complexity is naturally different. Regular writing needs to be analyzed according to the specific situation, a basic principle is: only write what is appropriate, not complex.
For date extraction, you can write the simplest rules as long as you can distinguish them from non-dates, such as
\ d {4} -\ d {2} -\ d {2}
If a date in yyyy-MM-dd format can be uniquely located in the source string, it can be used for extraction.
For verification, it doesn't make much sense to just verify the composition and format of the characters, but also add a check to the rules. Due to the existence of leap years, the checking regularity of dates becomes more complicated.
Let's first take a look at the valid range of dates and what a leap year is.
2 rules for dates
2.1 valid range of dates
For the valid range of dates, different application scenarios will vary.
The valid range for DateTime objects defined in MSDN is: 0001-01-01 00:00:00 to 9999-12-31 23:59:59.
The 0 of the UNIX timestamp is 1970-01-01T00:00:00Z according to the ISO 8601 specification.
In practical application, the range of dates basically does not exceed the range specified by DateTime, so canonical verification can take the range of dates commonly used.
2.2 what is a leap year
(the following is from Baidu encyclopedia)
The leap year (leap year) is established to make up for the time difference between the number of days of the year caused by the artificial calendar and the actual revolution cycle of the earth. The year that makes up the time difference is a leap year.
The earth moves around the sun in a period of 365 days, 5 hours, 48 minutes and 46 seconds (365.24219 days), that is, a regression year (tropical year). The average year of the Gregorian calendar is only 365 days, which is about 0.2422 days shorter than the return year, and it accumulates about one day every four years. Add this day to the end of February (that is, February 29), so that the length of the current year becomes 366 days, which is a leap year.
It should be noted that the current Gregorian calendar is based on the Roman Julian calendar. Because there was no understanding of the problem of having to calculate an extra 0.0078 days a year at that time, there were a total of 10 more days from 46 BC to the 16th century. For this reason, the then Pope Gregory XIII designated October 5, 1582 as October 15. And started a new leap year rule. That is to say, it is stipulated that the Gregorian calendar year is a full hundred, and it must be a multiple of 400 to be a leap year, and a multiple of 400 is a normal year. For example, 1700, 1800 and 1900 are normal years, and 2000 is a leap year. Since then, the average annual length is 365.2425 days, with a deviation of 1 day in about 4 years. On the basis of one leap year every four years, an average of 0.0078 more days will be calculated each year, and about three more days will be added after four hundred years, so there will be three fewer leap years every four hundred years. The calculation of leap years boils down to what is commonly said: one leap in four years; one leap in a hundred years; another leap in four hundred years.
2.3 format of date
The hyphens of dates vary from language to culture, and are 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 common way to write complex regularities is to separate irrelevant requirements, write corresponding regularities respectively, and then combine them to check their correlation and influence, and basically get the corresponding regularities.
According to the definition of a leap year, dates can be classified in several ways.
3.1.1 it is divided into two categories according to whether the number of days is related to the year.
The category that has nothing to do with the year can be subdivided into two categories according to the number of days in each month.
January, 3, 5, 7, 8, 10, December is from January to 31
April, June, September and November are from January to 30.
In the category related to the year
The usual February is from 1 to 28.
February in a leap year is from 1 to 29.
All months of all years contain 1-28 days
All years except February contain 29 and 30 days
All years 1, 3, 5, 7, 8, 10 and December contain 31
February in a leap year contains the 29th
3.1.2 it can be divided into four categories according to the inclusion date.
3.1.3 selection of classification methods
Because the implementation after date classification is realized through the branch structure (exp1 | exp2 | exp3), and the branch structure starts from the left branch to try to match to the right in turn, when one branch matches successfully, it no longer tries to the right, otherwise all branches are tried and the failure is reported.
The number of branches and the complexity of each branch will affect the matching efficiency. Considering the probability distribution of the verified date, most of them fall within 1-28 days, so using the second classification method will effectively improve the matching efficiency.
3.2 regular implementation
By using the classification method in Section 3.1.2, the corresponding rules can be written for each rule, which is temporarily implemented in MM-dd format.
First consider the first three rules that have nothing to do with the year, and the year can be written in a unified way.
(?! 0000) [0-9] {4}
Consider only the regularity of the month and day below
The months of all years, including ordinary years, include 1-28 days.
(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8])
All years, including normal years, include 29 and 30 except February.
(0 [13-9] | 1 [0-2])-(29 | 30)
All years 1, 3, 5, 7, 8, 10 and December, including normal years, include 31.
(0 [13578] | 1 [02])-31)
It adds up to all the dates except February 29th in the leap year.
(?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31)
Next, consider the realization of leap years.
February in a leap year contains the 29th
The month and day here are fixed, that is, 02-29, and only the year changes.
You can examine the rules by outputting all the leap year years with the following code
For (int I = 1; I < 10000; iTunes +) {if ((I% 4 = = 0 & & I% 100! = 0) | | I% 400 = = 0) {richTextBox2.Text + = string.Format ("{0if 0000}", I) + "\ n";}}
According to the rules of leap years, it is easy to sort out the rules, one leap every four years.
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])
No leap in a hundred years, another leap in four hundred years.
(0 [48] | [2468] [048] | [13579] [26]) 00
Together, it is February 29th of all leap years.
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [13579] [26]) 00)-02-29)
The four rules have been implemented and have no influence on each other. Together, they are the regularities of all dates that fall within the DateTime range.
^ (?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31) | (0 [0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48]) ] | [2468] [048] | [13579] [26]) 00)-02-29) $
Considering that this regular expression is only used for validation, the capture group is meaningless and will only take up resources and affect the matching efficiency, so you can use non-capture groups for optimization.
^ (?: (! 0000) [0-9] {4}-(: 0 [1-9] | 1 [0-2])-(?: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(?: 29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2} (?: 0 [48]) | 2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $
The above regular years 0001-9999, in the format yyyy-MM-dd. The validity and performance of the rule can be verified by the following code
DateTime dt = new DateTime (1,1,1); DateTime endDay = new DateTime (9999, 12,31); Stopwatch sw = new Stopwatch (); sw.Start () Regex dateRegex = new Regex (@ "^ (?: (! 0000) [0-9] {4}-(: (?: 0 [1-9] | 1 [0-2]))-(: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2}) ?: 0 [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $") / / Regex dateRegex = new Regex (@ "^ (?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31) | ([0-9] {2} (0 [48] | [2468] [048]) | 13579] [26]) | (0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $") Console.WriteLine ("start date:" + dt.ToString ("yyyy-MM-dd")); while (dt < endDay) {if (! dateRegex.IsMatch (dt.ToString ("yyyy-MM-dd") {Console.WriteLine (dt.ToString ("yyyy-MM-dd") + "false");} dt = dt.AddDays (1) } if (! dateRegex.IsMatch (dt.ToString ("yyyy-MM-dd")) {Console.WriteLine (dt.ToString ("yyyy-MM-dd") + "false");} Console.WriteLine ("end date:" + dt.ToString ("yyyy-MM-dd")); sw.Stop (); Console.WriteLine ("testing time:" + sw.ElapsedMilliseconds + "ms"); Console.WriteLine ("Test complete!") ; Console.ReadLine ()
4 date regular expression extension
4.1 expansion of the form of "year, month and day"
The above implementation is yyyy-MM-dd format date verification, taking into account the different hyphens, and the month and day may be M and d, that is, yyyy-M-d format, can be extended to the above rules
^ (?: (! 0000) [0-9] {4} ([- /.]) (?: (?: 0? [1-9] | 1 [0-2]) (?) (: 0? [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0? [13-9] | 1 [0-2]) ([- /.]) (? 29 | 30) | (?: 0? [13578] | 1 [02]) ) ([- /.]?) 31) | (?: [0-9] {2} (?) 0 [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00) ([- /.]?) 0room2 ([- /.]) 29)
Simplify with backreferences, year 0001-9999, format yyyy-MM-dd or yyyy-M-d, with no hyphen or "-", "/", "." One of them.
^ (?: (! 0000) [0-9] {4} ([- /.]) (?: (?: 0? [1-9] | 1 [0-2])\ 1 (?: 0? [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0? [13-9] | 1 [0-2])\ 1 (?: 29 | 30) | (?: 0? [13578] | 1 [02])\ 1 (?: 31) | : [0-9] {2} (?: 0 [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00) ([- /.]) 0room2\ 2 (?: 29)) $
This is the most complete regular form of "year, month and day". Different meaning parts are marked with different colors and can be cut according to their own needs.
4.2 other forms of expansion
After understanding the meaning of the above regular parts and the relationship between them, it is easy to expand the date regularity into other formats, such as the "day, month and year" format of dd/MM/yyyy.
^ (?: 0? [1-9] | 1 [0-9] | 2 [0-8]) ([- /.]) (?: 0? [1-9] | 1 [0-2]) | (?: 29 | 30) ([- /.]) (: 0? [13-9] | 1 [0-2]) | 31 ([- /.]) (?) (: 0? [13578] | 1 [02])) ([- /.]) (? 0000) [0-9] {4} | 29 ([- /.]?) 0room2 ([- /.]?) (?: [0-9] {2} (?: 0 [48] | [2468] [048] | [13579] [26]) | (: 0 [48] | [2468] [048] | [13579] [26]) 00)) $
One thing to note about this format is that it cannot be optimized with back references. Hyphens can be cut according to your own needs.
4.3 add an extension of time
The specification of time is very clear and simple, basically in the form of HH:mm:ss and H:m:s.
([01] [0-9] | 2 [0-3]): [0-5] [0-9]: [0-5] [0-9]
Fit into the regularity of the date, yyyy-MM-dd HH:mm:ss
^ (?: (! 0000) [0-9] {4}-(: 0 [1-9] | 1 [0-2])-(?: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(?: 29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2} (?: 0 [48]) | 2468] [048] | [13579] [26]) | (?) [2468] [048] | [13579] [26]) 00)-02-29)\ s + ([01] [0-9] | 2 [0-3]): [0-5] [0-9]: [0-5] [0-9] $
4.4 year customization
In all the above-mentioned years, 0001-9999 is used. Of course, the year can also be customized according to the leap year rules.
For example, year 1600-9999, format yyyy-MM-dd or yyyy-M-d, hyphen can be absent or "-", "/", "." One of them.
^ (?: 1 [6-9] | [2-9] [0-9]) [0-9] {2} ([- /.]) (?: (: 0? [1-9] | 1 [0-2])\ 1 (?: 0? [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0? [13-9])\ 1 (?: 29 | 30) | (?: 0? [13578]) | | 1 [02])\ 1 (?: 31) | (: (?) 1 [6-9] | [2-9] [0-9]) (?: 0 [48] | [2468] [048] | [13579] [26]) | (?: 16 | [2468] [048] | [3579] [26]) 00) ([- /.]?) 0room2\ 2 (?: 29)) $|
5 Special instructions
The above rules are the most basic regular syntax rules, which can be supported by most languages using traditional NFA engines, including JavaScript, Java, .NET and so on.
In addition, the requirement shows that, although the rules of the date are relatively clear and can be cut in this way to get the date regularity that meets the requirements, it is not recommended to use the regularity. The strength of the regularity lies in its flexibility, which can be tailored according to the demand. If it is only used to apply the template, then the regular is not called regular.
There are not many regular grammatical rules, and it is easy to get started. Mastering grammatical rules and tailoring clothes is the "Tao" of regularization.
6 Application
First of all, look at the demand.
Entry of date:
Manual input, you can enter two formats yyyymmdd or yyyy-mm-dd
Second, the solution.
The user enters the date manually and needs to verify the format of the date entered
The possible input situations of the user can be divided into the following categories:
(1)。 Input is blank or blank
(2)。 Enter a non-date format
According to the date format saved to the database, the saved format is yyyy-mm-dd, so the user needs to convert it to yyyy-mm-dd after entering yyyymmdd.
Train of thought:
To verify the date format, the first thing that comes to mind is the verification control of VS, but because there are dozens of controls that need to be validated, you need to pull controls one by one. If you need to modify them later, it is also troublesome to achieve control through JS, and then verify the date through regular expressions.
Third, JS implementation
/ / verify date function date (id) {var idvalue = document.getElementById (id) .value; / / remove spaces by looking for the element var tmpStr = ""; var strReturn = ""; / / call trim () to remove spaces, because js does not support trim () var iIdNo = trim (idvalue) / / regular expression to determine the date format, including the bounds of the date, the format of the date Var v = idvalue.match (/ ^ (1 [6-9] | [2-9]\ d)\ d {2})-(0? [13578] | 1 [02])-(0? [1-9] | [12]\ d | 3 [01]) | ((1 [6-9] | [2-9]\ d)\ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [ 12]\ d | 30)) | ((1 [6-9] | [2-9]\ d)\ d {2})-0room2-(0? [1-9] | 1\ d | 2 [0-8])) | ((1 [6-9] | [2-9]\ d) (0 [48] | [2468] [048] | [13579] [26])) | (16 | [2468] [048] | [3579] [26]) 00))-0room229 -) $/) / / skip detection if (iIdNo.length = = 0) {return false;} / / automatically change the date format to yyyy-mm-dd if (iIdNo.length = = 8) {tmpStr = iIdNo.substring (0,8); tmpStr = tmpStr.substring (0,4) + "-" + tmpStr.substring (4,6) + "-" + tmpStr.substring (6,8) document.getElementById (id). Value = tmpStr Document.getElementById (id). Focus ();} / / verify, determine the date format if ((iIdNo.length! = 8) & &! v) {strReturn = "date format error, prompt: 19990101 or 1999-01-01"; alert (strReturn); document.getElementById (id). Select (); return false }} / / use regular expressions to remove spaces at both ends of a string (because js does not support trim ()) function trim (str) {return str.replace (/ (^\ s *) | (\ swatch $) / g, ");} / / foreground call (get focus trigger)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.