Detailed Jiu Zheng The expression of the digital verification _ regular Expression

Source: Internet
Author: User

This blog post will introduce some commonly used digital verification methods, including integer verification, domestic phone number verification, ID number verification, as well as IP address verification, and so on authentication methods, if you are not familiar with the basic concepts, you can first look at the blog I wrote before.

The first part: numerical verification

1. Verify a string that contains only numbers, a specified length (N)

For example, I want to verify that a string that contains only a number and a length of 6, such as 123456, can be validated with the same effect as the following

Copy Code code as follows:

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

The above methods have the same effect, more recommended first, it is more concise! Note: I will use a more concise regular expression in the back, but not too verbose!

2. Verify word strings that contain only numbers, specified range length (n-m)

Like I'm going to verify a string that contains only numbers, 5 to 8 in length, such as 12345,123456,1234567,12345678, the authentication method you can use

3. Validation of nonnegative integers

Obviously 0,100,56 are non-negative integers, and -12,0135 are not non-negative integers, and the validation demo is as follows:

We can see that 021-56 is not selected because it is not a positive number. (Note: The ^ is used to indicate that it must begin with 0 or [1-9], so-56 is not selected; if not ^, 56 in 56 will be selected)

4. Verification of arbitrary integers

Any integer, such as 0,456,-65, etc. this is an arbitrary integer, which means we need to combine positive and negative integers, and the validation demo is as follows:

  

All positive numbers, including 0, positive and negative integers, are matched.

5. Validation of positive integers within a specified range

For example, we want to verify that the positive integers within the 1-5678 interval, such as 465,23,5677, are in this range, how do you verify that? Don't worry, we can verify the interval:

    • Use \b[1-9]\d{0-2}\b to verify that all positive integers between 1-999
    • Use \b[1-4]\d{3}\b to verify that all positive integers between 1000-4999
    • Use \b5[0-5]\d{2}\b to verify that all positive integers between 5000-5599
    • Use \b56[0-6]\d\b to verify that all positive integers between 5600-5669
    • Use \b567[0-8]\b to verify that all positive integers between 5670-5678

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

Copy Code code as follows:

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

But is that true? The verification is as follows:

  

How did it happen??? The next three number of verification is not what we want to effect AH!! This is because regular expressions match from left to right when they match, where 2602 and 4999 do not need to continue because they can complete a match using [1-9]\d{0,2}.

Shall we try the order of the regular expressions backwards? As shown below:

Copy Code code as follows:

^ (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:

This time we are happy that the number between 1-5678 has been selected!! But 5 of the 789 and-5 were chosen. This is because we have only added ^ before the first group, and we need to add ^ before each grouping. As shown below:

Copy Code code as follows:

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

The effect will be no problem, as follows:

So: the combination order principle (positive integer): A range combination of the maximum range of values, in turn, to the minimum value.

Inspired by this example, we can also solve the problem by adding $ or \b to each of the first examples of validation of a positive integer within a specified range.

That means the following two lines of code are valid:

Copy Code code as follows:

^ (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. Verification of real numbers

The validation of real numbers here is a real number that contains at least one decimal point, so the real numbers include integral parts, fractional parts, and decimal points.

The authentication method is as follows:

Copy Code code as follows:

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

Which--? means there can be a minus sign or minus sign, (0| [1-9]\d*) means that the integer part can be 0 or other integers that do not begin with 0, \. Is for the unary character. To escape, \d+ indicates that 1 or more digits can be duplicated after the decimal point. So it can be used to verify the general form of real numbers (such as 0.0, 1.2,-1.20, etc.), but also can be used to verify negative 0, such as-0.0, 0.00, and so on.

If we want to validate the specified precision of the real number, we just need to modify the end of the + to the appropriate precision, as follows:

Copy Code code as follows:

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

A real number that represents a decimal part length of 3.

7. Verification of scientific counting method

The scientific notation is the form of a number as a a*10^n. Where a is an integer or a decimal (such as 5,3.2, etc.) with only one integer, so it is known that 1<=|a|<10. and n is an integer. So it's not difficult to get the verification method of scientific notation as follows:

Copy Code code as follows:

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

Part Two: Verification of 4 domestic telephone numbers

We know that China's telephone number is in the form of the following four kinds:

1. Mobile phone number

2. Fixed telephone number (excluding area code)

3. Area code + fixed telephone number

4. Area code + Fixed telephone number + extension number

Let's introduce them in order

1. Mobile phone number

The current domestic mobile phone number is 13 start, 15 start and 18 start, and the third digit currently has "0-9" This 10 number, so the verification is very simple. As shown below:

Obviously the second method is simpler.

2. Fixed telephone number (excluding area code)

A fixed phone number is typically 7 bits (such as 2268358) or 8 bits (82668110), so it is very simple to verify, as follows:

However, the telephone number in a certain area is often fixed in a specific range, such as Xinjiang Shihezi a region of 2268001-2268999, then want to make sure it takes a little bit of effort.

We can divide the 2268001-2268999 into 2268001-2268009 and 2268010-2268099 and 2268100-2268999. In this way, the validation of the three regular expressions can be combined. As shown below:

Copy Code code as follows:

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

The effect is as follows:

PS here is not specifically introduced, are very simple knowledge, if you have questions to see my last blog, it is very specific to the basic knowledge.

3. Area code + fixed telephone number verification

The length of the area code is typically 3-4 digits, the length of the fixed phone number is typically 7-8 digits, for example, 029-82668110 is a combination of 3-digit area code and 8-digit fixed telephone number, and 0993-2268358 is a combination of 4-digit area code and 7-digit fixed number. and between the area code and the fixed number are generally by-(hyphen) link. We only need to verify the area code and the fixed phone separately.

Copy Code code as follows:

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

The demo effect is as follows:

4. Area code + Fixed telephone number verification + extension number verification

Some of the larger companies, businesses or government departments that provide a fixed phone number to the outside, in addition to the area code, fixed telephone number, may also include extension numbers.

Here's an example of a 4-bit rating number. Usually before the extension number may be a space, may be-(hyphen), there may be nothing. So the verification method is as follows:

Copy Code code as follows:

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

The demo effect is as follows:

Part III: Verification of 2 kinds of identification numbers

1. Basic knowledge

15-digit ID number:

1985 our country implements the identity card system, at that time issued the ID card number is 15 digits. The first 6 digits are the address code, the middle 6 is the birth date code (two digits each month and the year), and the last three digits are sequential codes.

(Note: The sequence code is to the same year, the same month, the same day the birth of the number of people, ordinal code of odd distribution to men, even assigned to women)

18-digit ID number:

1999 China began to use 18-digit identification number. The first 6 digits are the address code, the middle 8 is the birth date code (the year is 4 digits, the month day each uses 2 digits), the last four digits is the sequential code and the check code.

(Note: The 4-digit year is because using 2-bit causes conflicts, such as those born in 1903 and 2003.) The check code is mainly to verify that the computer input the first 17 digits of the Citizen ID number is correct, its value range is 0 to 10, the duty is equal to 10 o'clock, with X to express

2. Verification of 15-digit ID number

The first 6-bit address code can be any number, 78-bit year code is any number, 9 and 10-bit month codes should be between 01-12, 11 and 12-bit date codes are between 01-31, and the last three-bit sequence code is any string with a length of 3. So the verification method is as follows:

Copy Code code as follows:

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

3. Verification of 18-digit ID number

The first 6-bit address code is any number, the 7-10-bit year code begins with a 19 or 20 (No 18 start), an ID number with 15 digits on the day, and a three-bit sequence of any string with a length of 3, and finally the verification code is 0-9 or X. So the verification method is as follows:

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

Because as long as you know the thinking, regular expression is not difficult to write, so here is not detailed.

Part IV: Verification of ZIP code

Coding rules for China's postcode: China's four-level six-bit coding system, the first two said provinces, municipalities, autonomous regions, the third representative postal area, the fourth representative of counties, cities, the last two representatives to send the post office, the last two is representative from the city which delivery area, that is, the location of the delivery area. For example: Postal code "130021" "13" on behalf of Jilin Province, "00" on behalf of the capital Changchun, "21" on behalf of the delivery area.

Therefore, the verification of our country's ZIP code is very convenient, as follows:

Part five: Authentication of two IP addresses

IP addresses can be easily validated, and can be accurately validated.

1. Simple IP Address Verification

I first ping to the http://www.jb51.net/IP address: 42.121.252.58. In fact, IP addresses are generally 1~3-bit integers. 1~3-bit integers. 1~3-bit integers. 1~3-bit integers, so we can do simple validation with the following regular expression:

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

The validation effect looks like this:

2. Accurate IP Address Verification

Obviously, the simple IP address verification above is imprecise, such as 999.999.999.999, which is not a correct IP address.

We know that every number of 32-bit IP addresses is between 0~255, so for 1~3-bit integers. 1~3-bit integers. 1~3-bit integers. 1~3-bit integers we should limit the integers to the 0~255, obviously, using the partitioned method.

0-99 can be expressed in this way: ([1-9]\d?) | 0 (Note the notation here, if this number is not 0, then there can be no 0 in front)

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

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

250-255 can be expressed in this way: 25[0-5]

So to sum up, you can get accurate IP address verification methods are as follows:

Copy Code code 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 demo effect is as follows:

It is important to note that grouping is crucial, and only the group is divided to be possible without problems.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.