一、校验数字的表达式
-
数字:
^[0-9]*$
-
n位的数字:
^\d{n}$
-
至少n位的数字:
^\d{n,}$
-
m-n位的数字:
^\d{m,n}$
-
零和非零开头的数字:
^(0|[1-9][0-9]*)$
-
非零开头的最多带两位小数的数字:
^([1-9][0-9]*)+(.[0-9]{1,2})?$
-
带1-2位小数的正数或负数:
^(\-)?\d+(\.\d{1,2})?$
-
正数、负数、和小数:
^(\-|\+)?\d+(\.\d+)?$
-
有两位小数的正实数:
^[0-9]+(.[0-9]{2})?$
-
有1~3位小数的正实数:
^[0-9]+(.[0-9]{1,3})?$
-
非零的正整数:
^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
-
非零的负整数:
^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
-
非负整数:
^\d+$ 或 ^[1-9]\d*|0$
-
非正整数:
^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
-
非负浮点数:
^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
-
非正浮点数:
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
-
正浮点数:
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
-
负浮点数:
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
-
浮点数:
^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
上述正则 有待补充钱包,邮箱等常见的正则
ipv6正则表达式解析
? 既然看到了上述的正则,下午看了下对ipv6的正则也可以进行解析,ipv4长度为32位,ipv6的长度为128位,ipv6极大扩展了ip地址,如果地址的分配的速率是每微秒100万,那么需要10A19年才能将所有地址分配完毕。
零压缩和嵌入式写法
? ipv6地址表示,ipv6将地址通常写成8组,例如:CD00:0000:0000:0000:ABCD:0000:ACBD:0A01,这其中的0可以通过::来进行简单的表示,上述地址则为 CD00::ABCD:0000:ACBD:0A01 因为无法对所有的位数进行判断,所以只能用1次。一个ipv6地址可以将一组ipv4地址内嵌进去,成为ipv6和ipv4的混合体。ipv6对ipv4有两种嵌套的方式,ipv4映像地址和ipv4兼容地址
? ipv4映像地址:0000:0000:0000:0000:0000:ffff:192.168.89.9这种混合写法对应的ipv6地址:0000:0000:0000:0000:0000:ffff:c0a8:5909其实表示的是192.168.89.9这个ipv4地址。ipv4映像地址布局如下:0000…..0000(80bits)| FFFF | IPv4 address |
? ipv4兼容地址已经启用,所以不进行过多表述。
前面7个完整的字段,第八个字段为0压缩写法或者数字。如:
? 1:2:3:4:5:6:7:: -> (([0-9A-Fa-f]{1,4}??{7}([0-9A-Fa-f]{1,4}{1}|:))
前面6个完整的字段,后面两个字段可能为0压缩写法或者嵌入式写法。如:
? 1:2:3:4:5:6::8 -> (([0-9A-Fa-f]{1,4}??{6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2 [0-1] [0-9]| [0-1] [0-9] [0-9]|0 [1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2 [0-4] [0-9]| [0-1] [0-9] [0-9]|0 [1 -9] [0-9]|([0-9])]{1,2})){3})|:))
前面有5个完整的字段,后面三个字段可能为零压缩写法或者ipv4嵌入式写法
? 1:2:3:4:5:: -> (([0-9A-Fa-f]{1,4}??{5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2 [0-1] [0-9]| [0-1] [0-9] [0-9]|0 [1 -9] [0-9]|([0-9])]{1,2})( [.] ( 25 [0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))
? 所以ipv6的正则表达式为
(([0-9A-Fa-f]{1,4}??{7}([0-9A-Fa-f]{1,4}{1}|:))|
(([0-9A-Fa-f]{1,4}??{6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(([0-9A-Fa-f]{1,4}??{5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(([0-9A-Fa-f]{1,4}??{4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(([0-9A-Fa-f]{1,4}??{3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(([0-9A-Fa-f]{1,4}??{2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(([0-9A-Fa-f]{1,4}??{1}(:[0-9A-Fa-f]{1,4}{1,6}|((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
(??:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|
? 删除换行符为
(([0-9A-Fa-f]{1,4}??{7}([0-9A-Fa-f]{1,4}{1}|:))|(([0-9A-Fa-f]{1,4}??{6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}??{5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}??{4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}??{3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}??{2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}??{1}(:[0-9A-Fa-f]{1,4}{1,6}|((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|(??:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})([.] (25[0-5]|2[0-4] [0-9]|[0-1] [0-9] [0-9]|0[1 -9] [0-9]|([0-9])]{1,2})){3})|:))|