Easily implement regular verification of specific digital ranges with regrange, such as time, IP, etc.

zhaozj2021-02-16  54

I often see Zheng Ze verification similar to the following 1. Time ^ (0 | [1-9] / D {0, 2} | [0-1] / d {3} | 20 / d {2} | 20 [ 0-4] / D | 2050) - (/ D | 0 / D | 1 [0-2]) - (/ D | [0-2] / D | 3 [0-1]) ((/ D | [0-1] / D | 2 [0-3]): (/ D | [0-4] / D | 5 [0-9]) (: (/ d | [0-4] / d | 5 [0-9]))?) $ 2.IP address ^ (0 | [1-9] / d? | [0-1] / d {2} | 2 [0-4] / d | 25 [0 -5]) /. (0 | [1-9] / d? | [0-1] / d {2} | 2 [0-4] / d | 25 [0-5]) /. (0 | [1-9] / d? | [0-1] / d {2} | 2 [0-4] / d | 25 [0-5]) /. (0 | [1-9] / d? | [0-1] / d {2} | 2 [0-4] / d | 25 [0-5]) $

It seems that people who need certain regular knowledge can be written, but now I use regrange. You only need to write symbols outside the number range, such as "-", ":", "."

1. Time string regdate = regRANGE (2050) "-" Regrange (12) " " String Regtime = regRANGE (23) ": regRANGE (59) " (: " Regrange (59) ")?"; string regdatetime = "^" regdate "(" regtime ")?"; 2.ip address string regip = "^" regrange (255) "/ /. " Regrange (255) " //. " regRANGE (255) ///." regRANGE (255) "$";

Is it very simple?

The RegRange code is as follows

String Regrange (INT T) {/ ********** (QiushuiWuhen 2002-12-12) *********** / STRING S = T.toString (); int P, L = S.LENGTH; STRINGBUILDER RET = New StringBuilder ("); if (l> 2) Ret.Append (" 0 | [1-9] // D " ((L> 3)?" {0 , " (L-2) "} ":"? ") " | "); ELSE IF (L> 1) RET.APPEND (" // D | "); for (int i = 0; i 0) RET.APPpend ("|" s.SUBSTRING (0, i)); p = s [i] -48; if (li> 1) ret.Append (((( (p> 1)? "[0-" (p-1) "]": "0") ((li> 2)? "// d {" (li-1) "}" : "// D")); else ret.Append ((p> 0)? "[0-" p "]": "0") ")");} Return Ret.Tostring () ;}example:

For (int i = 0; i <33; i ) if (! regex.ismatch ("2002-12 -" i, regdatetime)) Response.write (i "error
"); for (int i = 0; I <257; i ) IF (! Regex.ismatch ("127.0.0." I, regip)) response.write (i "error
");

转载请注明原文地址:https://www.9cbs.com/read-28054.html

New Post(0)