Home  >  Article  >  Web Front-end  >  Detailed explanation of regular digital verification (with code)

Detailed explanation of regular digital verification (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 14:21:153939browse

This time I bring to you, what are the precautions, the following is a practical case, let's take a look.

Part 1: Numerical Verification

#1. Verify strings containing only numbers and specified length (N)

For example, if I want to verify a string containing only numbers and a length of 6, such as 123456, I can use the following verification methods with the same effect

\d{6}
[0-9]{6}
\d\d\d\d\d\d

The above Both methods have the same effect, and we recommend the first one, which is more concise! Note: I will use the more concise regular expression later, without being too verbose!

2. Verify that the word string contains only numbers and specifies the range length (N-M)

For example, I want to verify that it only contains numbers and the length is between 5 and 8 Strings, such as 12345, 123456, 1234567, 12345678, you can use the verification method

3. Verification of non-negative integers

Obviously 0, 100, 56, etc. are all non-negative integers, and -12, 0135, etc. are not non-negative integers. The verification demonstration is as follows:

We can see 021 - 56 will not be selected since it is not a positive number. (Note: ^ is used, which means it must start with 0 or [1-9], so -56 is not selected; if there is no ^, then 56 in -56 will be selected)

4. Verification of arbitrary integers

Any integers such as 0, 456, -65, etc. This is an arbitrary integer, which means we need to combine positive numbers and non-negative integers. The verification demonstration is as follows:

 

So all positive numbers including 0, positive integers and negative integers are matched.

5. Verification of positive integers within the specified range

For example, we want to verify positive integers in the range of 1-5678, such as 465, 23, 5677, etc. It belongs to this range, how to verify this? Don’t worry, we can verify between partitions:

  • Use \b[1-9]\d{0-2}\b to verify all positive integers between 1-999

  • Use \b[1-4]\d{3}\b to verify all positive integers between 1000-4999

  • Use \b5[0-5]\d{2}\bTo verify all positive integers between 5000-5599

  • Use \b56[0-6]\d\b to Verify all positive integers between 5600-5669

  • Use \b567[0-8]\b to verify all positive integers between 5670-5678

To sum up, we can use the following regular expression to verify all positive integers between 1-5678:

^([1-9]\d{0,2})|([1-4]\d{3})|(5[0-5]\d{2})|(56[0-6]\d)|(567[0-8])$

But is this really the case? The verification is as follows:

 

Why is it like this? ? ? The verification of the next three numbers is not the effect we want! ! This is because the regular expression will match from left to right when matching. Among them, 2602 and 4999 can be matched using [1-9]\d{0,2}, so there is no need to continue.

Let’s try reversing the order of the regular expressions? As shown below:

^(567[0-8])|(56[0-6]\d)|(5[0-5]\d{2})|([1-4]\d{3})|([1-9]\d{0,2})$

The effect is as follows:

What makes us happy this time is that all numbers between 1-5678 have been selected! ! But 5 out of 789 and -5 were selected. This is because we only added ^ before the first grouping. What we need to do is add ^ before each grouping. As shown below:

^(567[0-8])|^(56[0-6]\d)|^(5[0-5]\d{2})|^([1-4]\d{3})|^([1-9]\d{0,2})$

There is no problem with the effect this time, as follows:

It can be seen from this: the principle of combination order (positive integer): from The range combination of the maximum value to the range combination of the minimum value.

从这个例子受到启发,我们对于指定范围内的正整数的验证的第一个例子中的每一个分组后添加$或\b也可以解决问题。

也就是说下面的两行代码均有效:

^(567[0-8])|^(56[0-6]\d)|^(5[0-5]\d{2})|^([1-4]\d{3})|^([1-9]\d{0,2})$
^([1-9]\d{0,2})$|([1-4]\d{3})$|(5[0-5]\d{2})$|(56[0-6]\d)$|(567[0-8])$

6.实数的验证

这里要介绍的实数的验证是至少包含一个小数点的实数,因此实数就包括了整数部分、小数部分和小数点。

 验证方法如下:

-?(0|([1-9]\d*))\.\d+

其中-?表示可以有负号也可以没有负号,(0|[1-9]\d*)表示整数部分可以是0也可以是不以0开头的其他整数,\.是为了对元字符.进行转义,\d+表示在小数点后面可以有1个或多个数字重复。所以它可以用来验证一般形式的实数(如0.0、1.2、-1.20等),还可以用来验证负0,如-0.0、-0.00等。

如果我们希望验证指定精度的实数,我们只需要把末尾的+修改成相应的精度即可,如下所示:

-?(0|([1-9]\d*))\.\d{3}$

即表示小数部分长度为3的实数。

7.科学计数法的验证

科学计数法就是把一个数记成a*10^n的形式。其中,a是一位整数或着是只有一位整数的小数(如5,3.2等等),所以可知1<=|a|<10。而n是一位整数。所以不难得出科学计数法的验证方法如下所示:

^-?[1-9](\.\d+)?\*10\^-?\d+$

第二部分:4种国内电话号码的验证

我们知道中国的电话号码的形式不外乎有下面四种:

1.手机号码

2.固定电话号码(不包括区号)

3.区号+固定电话号码

4.区号+固定电话号码+分机号

下面我们按照顺序逐一介绍

1.手机号码

目前国内的手机号码多是13开头、15开头和18开头,并且第三位数字目前都有【0-9】这10个数,所以验证起来就很简单了。如下所示:

     

显然第二种方法更简单一些。

 2.固定电话号码(不包括区号)

固定电话号码一般为7位(如2268358)或8位(82668110),所以验证起来是非常简单的,如下所示:

 

但是,某个地区的电话号码往往是固定在一个具体的范围里的,比如新疆石河子某个地区为2268001-2268999,这时想要确定就需要稍微花一些功夫了。

我们可以把2268001-2268999划分为2268001-2268009和2268010-2268099和2268100-2268999。这样,把验证三者的正则表达式组合起来即可。如下所示:

2268((00[1-9])|(0[1-9]\d)|([1-9]\d{2}))

 

PS 这里就不具体介绍啦,都是很简单的知识,如果有疑问可以看我的上一篇博文,它对基本知识阐述得很具体。

3.区号+固定电话号码验证

区号的长度一般为3-4位,固定电话号码的长度一般为7-8位,比如029-82668110为3位区号和8位固定电话号码的组合,0993-2268358是4位区号和7位固定号码的组合。且在区号和固定号码之间一般都是由-(连字符)链接的。可知,我们只需要对区号和固定电话分别验证即可。  

\b0\d{2,3}[- ]?\d{7,8}\b

演示效果如下:

 

4.区号+固定电话号码验证+分机号码验证

一些比较大的公司、企业或者政府部门在向外部提供固定的电话号码是,除了区号、固定电话号码之外,还可能包括分机号码。

下面我们以4位的分级号码为例。一般在分机号码之前可能是空格,也可能是-(连字符),还可能什么都没有。 于是验证方法如下:

\b0\d{2,3}[- ]?\d{7,8}[- ]?\d{4}\b

演示效果如下:

    

第三部分:2种身份证号码的验证

1.基本知识

15位身份证号码:

1985年我国实行居民身份证制度,当时签发的身份证号码是15位的。其中前6位为地址码,中间6位为出生日期码(年月日各用两位数字表示),最后三位为顺序码。

(注:顺序码是对同年、同月、同日出生的人编订序号,顺序码的奇数分配给男性,偶数分配给女性)

18位身份证号码:

1999年我国开始使用18位的身份证号。其中前6位为地址码,中间8位为出生日期码(年用4位表示,月日各用2位表示),最后四位为顺序码和校验码。

(注:年份用4位是因为使用2位会导致冲突,比如1903年和2003年出生的人。而校验码主要是为了校验计算机输入公民身份证号码的前17位数字是否正确,其取值范围是0至10,当值等于10时,用X表示)

 2. 15位身份证号码的验证

前6位地址码可以为任意数字,78位的年份码为任意数字,9和10位的月份码应当在01-12之间,11和12位的日期码在01-31之间,最后三位的顺序码为长度为3的任意字符串。于是验证方法如下:

复制代码 代码如下:


\b\d{8}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}\b

 3. 18位身份证号码的验证

前6位地址码为任意数字,7-10位的年份码前两位以19或20开头(这里就不考虑18开头了),月日同15位的身份证号码,三位顺序码为长度为3的任意字符串,最后以为验证码为0-9或X。于是验证方法如下:

\b\d{6}(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)\b

因为只要知道了思路,正则表达式写出来并不难,所以这里就不细讲了。

 第四部分:邮政编码验证

我国邮政编码的编码规则:我国采用四级六位编码制,前两位表示省、市、自治区,第三位代表邮区,第四位代表县、市,最后两位代表投递邮局,最后两位是代表从这个城市哪个投递区投递的,即投递区的位置。例如:邮政编码“130021”“13”代表吉林省,“00”代表省会长春,“21”代表所在投递区。

因此验证我国的邮政编码就十分方便了,如下所示:

     

第五部分:两种IP地址的验证

IP地址可以简单验证,还可以精确验证。

1.简单IP地址验证

我首先ping到了http://www.jb51.net/的IP地址:42.121.252.58。 实际上IP地址一般是1~3位整数.1~3位整数.1~3位整数.1~3位整数,于是我们可以通过下面的正则表达式作简单验证:

([1-9]\d{0,2}\.){3}[1-9]\d{0,2}

验证效果如下所示:

 

2.精确IP地址验证

显然,上面的简单IP地址验证是不精确的,比如999.999.999.999这就不是一个正确的IP地址。

我们知道32位IP地址的每一个数值都是在0~255之间,所以对于1~3位整数.1~3位整数.1~3位整数.1~3位整数我们应该将整数限制在0~255之间,显然,这里要使用分区间的方法了。

0-99之间可以这样表示:([1-9]\d?)|0              (注意这里的表示方法,如果这个数不为0,那么前面就不能有0)

100-199 can be expressed like this: 1\d{2}

200-249 can be expressed like this: 2[0-4]\d

250-255 The time can be expressed like this: 25[0-5]

So to sum up, the method to get the accurate IP address verification is as follows:

Copy code The code is as follows:


(((25[0-5])|2[0-4]\d|1\d{2}|[1-9]\d|0)\.){ 3}((25[0-5])|2[0-4]\d|1\d{2}|[1-9]\d|0)

The demonstration effect is as follows:

It is worth noting that grouping is crucial. Only by dividing into the right groups can there be no problems.

The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support Script House.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of the g modifier of the regular global matching pattern

Regular expression m modifier ( Multi-line matching) detailed explanation

The above is the detailed content of Detailed explanation of regular digital verification (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn