帶閏年判斷的Regex

來源:互聯網
上載者:User
 

作者: xixigongzhu(夕夕公主)      http://search.csdn.net/Expert/topic/1974/1974227.xml?temp=.6640131

首先,你的年的範圍是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種Regex用|串連,然後加上潤年的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種Regex用|串連,然後加上非潤年2月和其他月份的處理:
String noleap = "(" + noleap1 + "|" + noleap2 + "|" + noleap3 + ")-("
                + month + "|" + other1 + "|" + other2 + ")";
  

潤年和非潤年的Regex的組合就是了:
String yearregex = leap + "|" + noleap;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.