C#中怎么使用正則表達(dá)式判斷輸入日期格式是否正確,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
c 正則表達(dá)式日期代碼一:
/// <summary> /// 是否為日期型字符串 /// </summary> /// <param name="StrSource">日期字符串(2008-05-08)</param> /// <returns></returns> public static bool IsDate(string StrSource) { return Regex.IsMatch(StrSource, @"^((((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})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"); } /// <summary> /// 是否為時(shí)間型字符串 /// </summary> /// <param name="source">時(shí)間字符串(15:00:00)</param> /// <returns></returns> public static bool IsTime(string StrSource) { return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$"); } /// <summary> /// 是否為日期+時(shí)間型字符串 /// </summary> /// <param name="source"></param> /// <returns></returns> public static bool IsDateTime(string StrSource) { return Regex.IsMatch(StrSource, @"^(((((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})-0?2-(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))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ "); }
c 正則表達(dá)式日期代碼二:
//是否是整數(shù) public static bool IsInt(string StrSource) { return Regex.IsMatch(StrSource, @"^[0-9]*$"); } public static bool IsDate(string strDate) { if (string.IsNullOrEmpty(strDate)) return false; string s_reg = @"^(?ni:(?=\\d)((?'year'((1[6-9])|([2-9]\\d))\\d\\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\\2 (?'day'((?<!(\\2((0?[2469])|11)\\2))31)|(?<!\\2(0?2)\\2)(29|30)|((?<=((1[6-9]|[2-9]\\d)(0[48]| [2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\\2\\3\\2)29)|((0?[1-9])|(1\\d)|(2[0- 8])))(?:(?=\\x20\\d)\\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]?\\d){0,2}(\\x20[AP]M))|([01]? \\d|2[0-3])(:[0-5]?\\d){1,2}))?)$"; Regex reg = new Regex(s_reg); if (reg.IsMatch(strDate.ToLower())) return true; else return false; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
本文名稱:C#中怎么使用正則表達(dá)式判斷輸入日期格式是否正確-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article22/pjhcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、ChatGPT、網(wǎng)站導(dǎo)航、商城網(wǎng)站、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容