程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 帶閏年判斷的正則表達式

帶閏年判斷的正則表達式

編輯:關於C語言
 

首先,你的年的范圍是1800-3999
潤年:
1800,1900,2100,2200,2300,2500,2600,2700,2900,3000,3100,3300,3400,3500,3700,3800,3900是個特殊值(能被4整除但不是潤年),要分出來:
String leap1 = "(((1[8-9])|([2-3][0-9]))(0|2|4|6|8)(4|8))";
String leap2 = "(((2(0|4|8))|(3(2|6)))00)";
其他的0結尾的:
String leap3 = "(((1[8-9])|([2-3][0-9]))(2|4|6|8)0)";
2,6結尾的:
String leap4 = "(((1[8-9])|([2-3][0-9]))(1|3|5|7|9)(2|6))";
潤年2月:
String leapmonth = "(02-(([0-1][1-9])|(10)|(2[0-8])))";
其它月份:
String other1 = "(((01)|(0[3-9])|(1[0-2]))-(([0-2][1-9])|([1-3]0)))";
String other2 = "(((0(1|3|5|7|8))|(10)|(12))-(31))";
把上面4種正則表達式用|連接,然後加上潤年的2月和其他月份的處理:
String leap = "(" + leap1 + "|" + leap2 + "|" + leap3 + "|" + leap4 + ")-("
+ leapmonth + "|" + other1 + "|" + other2 + ")";

非潤年:
String noleap1 = "(((1[8-9])|([2-3][0-9]))(0|2|4|6|8)(1|2|3|5|6|7|9))";
String noleap2 = "(((1[8-9])|(2(1|2|3|5|6|7|9))|(3(0|1|3|4|5|7|8|9)))00)";
String noleap3 = "(((1[8-9])|([2-3][0-9]))(1|3|5|7|9)(0|1|3|4|5|7|8|9))";
非潤年2月:
String month = "(02-(([0-2][1-9])|([1-2]0)))";
把上面3種正則表達式用|連接,然後加上非潤年2月和其他月份的處理:
String noleap = "(" + noleap1 + "|" + noleap2 + "|" + noleap3 + ")-("
+ month + "|" + other1 + "|" + other2 + ")";
 

潤年和非潤年的正則表達式的組合就是了:
String yearregex = leap + "|" + noleap;
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved