当前位置:网站首页>The most complete regular practical guide of the whole network. You're welcome to take it away

The most complete regular practical guide of the whole network. You're welcome to take it away

2022-07-05 00:37:00 glacier

Hello everyone , I'm glacier ~~

Recently, many friends asked me why I have so much time to write articles , Video recording , ok , Today I will share with you some gadgets that I often use in my daily work .

It took me two days to sort this out, using common regular expressions , Master these , You can have a lot less code than others , So you have more time to do your own things , Guys, take it , You're welcome ~

This time I have shared the regular expressions that I often use in my work , It's the mastery of these regular expressions , Glacier writes less than others every day 200 Line code , It greatly improves R & D efficiency , I suggest you collect it , Usually try to use it in your own project !!

Good command of regular expressions , Can help programmers write the most elegant code with the fastest speed .

Glacier in years of programming work , The regular expressions used are sorted out and summarized , These regular expressions can help you save a lot of coding time , Often a simple regular expression can omit a lot of if...else... Code .

This time, , Binghe disclosed his regular expressions to his friends , Hope to be able to bring substantial help to the partners .

Glaciers commonly used

An integer or a decimal

^[0-9]+\.{
    0,1}[0-9]{
    0,2}$ 

You can only enter Numbers

^[0-9]*$

Input only n Digit number

^\d{
    n}$

You can only enter at least n Digit number

^\d{
    n,}$

Input only m~n Digit number

^\d{
    m,n}$ 

Only numbers beginning with zero and non-zero can be entered

^(0|[1-9][0-9]*)$

Only positive real numbers with two decimal places can be entered

^[0-9]+(.[0-9]{
    2})?$

Only input has 1~3 Positive real number of decimal places

^[0-9]+(.[0-9]{
    1,3})?$

Only non-zero positive integers can be entered

^\+?[1-9][0-9]*$

Only non-zero negative integers can be entered

^\-[1-9][]0-9*$

You can only enter a length of 3 The characters of

^.{
    3}$

Only input from 26 A string of English letters

^[A-Za-z]+$

Only input from 26 A string of uppercase letters

^[A-Z]+$

Only input from 26 A string of lowercase letters

^[a-z]+$

Only numbers and 26 A string of English letters

^[A-Za-z0-9]+$

Only numbers can be entered 、26 A string of English letters or underscores

^\w+$

Verify user password :

^[a-zA-Z]\w{
    5,17}$ 

notes : The correct format is : Start with a letter , The length is in 6~18 Between , Can only contain characters 、 Numbers and underscores .

Verify that it contains ^%&',;=?$\ Equal character

[^%&',;=?$\x22]+ 

Only Chinese characters can be entered

^[\u4e00-\u9fa5]{
    0,}$ 

verification Email Address

^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

verification Internet URL

^[http|https]://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$

Verify phone number

^(\(\d{
    3,4}-)|\d{
    3.4}-)?\d{
    7,8}$

The correct format is :XXX-XXXXXXX、XXXX- XXXXXXXX、XXX-XXXXXXX、XXX-XXXXXXXX、XXXXXXX and XXXXXXXX

Authentication ID No (15 Bit or 18 Digit number )

^\d{
    15}|\d{
    18}$

Verify one year's 12 Months

^(0?[1-9]|1[0-2])$

The correct format is :01~09 and 1~12

Verify one month's 31 God

^((0?[1-9])|((1|2)[0-9])|30|31)$

The correct format is ;01~09 and 1~31

Regular expressions matching Chinese characters

[\u4e00-\u9fa5]

Match double byte characters ( Including Chinese characters )

[^\x00-\xff] 

Regular expressions matching empty rows

\n[\s| ]*\r

matching html Regular expressions for tags

<(.*)>(.*)<\/(.*)>|<(.*)\/>

Regular expression matching the leading and trailing spaces

(^\s*)|(\s*$)

matching Email Regular expression of address

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

matching HTML Tagged regular expression

<(\S*?)[^>]*>.*?|<.*? />

Commentary : The version circulating on the Internet is too bad , The above one can only match the part , There's nothing we can do about complex nested tags

A regular expression that matches the first and last whitespace characters

^\s*|\s*$

Commentary : Can be used to delete blank characters at the beginning and end of a line ( Including Spaces 、 tabs 、 Page breaks and so on ), Very useful expressions

matching Email Regular expression of address

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Commentary : Form validation is very practical

Match URL URL Regular expression of

[a-zA-z]+://[^\s]*

Commentary : The function of the version circulated on the Internet is very limited , The above can basically meet the needs

Whether the matching account is legal ( Beginning of letter , allow 5-16 byte , Allow alphanumeric underscores )

^[a-zA-Z][a-zA-Z0-9_]{
    4,15}$

Commentary : Form validation is very practical

Match domestic phone number

\d{
    3}-\d{
    8}|\d{
    4}-\d{
    7}

Commentary : The matching form is as follows 0511-4405222 or 021-87888822

Matching Tencent QQ Number

[1-9][0-9]{
    4,}

Commentary : tencent QQ Number from 10000 Start

Match China zip code

[1-9]\d{
    5}(?!\d)

Commentary : China Post code is 6 Digit number

Match ID

\d{
    15}|\d{
    18}

Commentary : China's ID card is 15 Bit or 18 position

matching ip Address

\d+\.\d+\.\d+\.\d+

Commentary : extract ip Useful for addresses

Match specific numbers

^[1-9]\d*$ // Matching positive integer 
^-[1-9]\d*$ // Matching negative integer 
^-?[1-9]\d*$ // Matching integer 
^[1-9]\d*|0$ // Match non negative integers ( Positive integer  + 0)
^-[1-9]\d*|0$ // Match non positive integers ( Negtive integer  + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ // Match positive floating point 
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ // Match negative float 
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ // Match floating point 
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ // Match non negative floating-point numbers ( Positive floating point  + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$// Match non positive floating-point numbers ( Negative floating point number  + 0)s

Commentary : Useful when dealing with large amounts of data , Pay attention to correction in specific application .

Match specific string

^[A-Za-z]+$// Match by 26 A string of English letters 
^[A-Z]+$// Match by 26 A string of uppercase letters 
^[a-z]+$// Match by 26 A string of lowercase letters 
^[A-Za-z0-9]+$// Match by numbers and 26 A string of English letters 
^\w+$// Match by number 、26 A string of English letters or underscores 

Commentary : The most basic and commonly used expressions

Verify password strength For example, the strength of the password is : Contains a combination of uppercase and lowercase letters and numbers , Special characters cannot be used , The length is in 8-10 Between .

^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{
    8,10}$

Check string

chinese .

^[\\u4e00-\\u9fa5]{
    0,}$

By digital 、26 A string of English letters or underscores

^\\w+$

check E-Mail Address

[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?

Verify ID number 15 position :

^[1-9]\\d{
    7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{
    3}$

18 position :

^[1-9]\\d{
    5}[1-9]\\d{
    3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{
    3}([0-9]|X)$

Verification date “yyyy-mm-dd“ Format date verification , A flat leap year has been considered .

^(?:(?!0000)[0-9]{
    4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{
    2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$

Verification amount Accurate to 2 Decimal place .

^[0-9]+(.[0-9]{
    2})?$

Check cell phone number Here is the domestic 13、15、18 Regular expression of mobile phone number at the beginning .( The first two numbers can be expanded according to the current domestic collection number )

^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{
    8}$

Judge IE Version of

^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$

check IP-v4 Address

\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){
    3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b

check IP-v6 Address

(([0-9a-fA-F]{
    1,4}:){
    7,7}[0-9a-fA-F]{
    1,4}|([0-9a-fA-F]{
    1,4}:){
    1,7}:|([0-9a-fA-F]{
    1,4}:){
    1,6}:[0-9a-fA-F]{
    1,4}|([0-9a-fA-F]{
    1,4}:){
    1,5}(:[0-9a-fA-F]{
    1,4}){
    1,2}|([0-9a-fA-F]{
    1,4}:){
    1,4}(:[0-9a-fA-F]{
    1,4}){
    1,3}|([0-9a-fA-F]{
    1,4}:){
    1,3}(:[0-9a-fA-F]{
    1,4}){
    1,4}|([0-9a-fA-F]{
    1,4}:){
    1,2}(:[0-9a-fA-F]{
    1,4}){
    1,5}|[0-9a-fA-F]{
    1,4}:((:[0-9a-fA-F]{
    1,4}){
    1,6})|:((:[0-9a-fA-F]{
    1,4}){
    1,7}|:)|fe80:(:[0-9a-fA-F]{
    0,4}){
    0,4}%[0-9a-zA-Z]{
    1,}|::(ffff(:0{
    1,4}){
    0,1}:){
    0,1}((25[0-5]|(2[0-4]|1{
    0,1}[0-9]){
    0,1}[0-9])\\.){
    3,3}(25[0-5]|(2[0-4]|1{
    0,1}[0-9]){
    0,1}[0-9])|([0-9a-fA-F]{
    1,4}:){
    1,4}:((25[0-5]|(2[0-4]|1{
    0,1}[0-9]){
    0,1}[0-9])\\.){
    3,3}(25[0-5]|(2[0-4]|1{
    0,1}[0-9]){
    0,1}[0-9]))

Check URL The prefix of

Many times in application development, it is necessary to distinguish between requests HTTPS still HTTP, You can take out a url And then make a logical judgment .

if (!s.match(/^[a-zA-Z]+:\\/\\//))
{
    
    s = 'http://' + s;
}

extract URL link

The following expression can filter out the URL.

^(f|ht){
    1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?

File path and extension verification verification windows Next file path and extension ( In the following example is .txt file )

^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$

Extract web page color code Sometimes we need to extract the color code from the web page , You can use the following expression .

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Take pictures of web pages

\\< *[img][^\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)

Extract page hyperlinks

(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>

lookup CSS attribute

^\\s*[a-zA-Z\\-]+\\s*[:]{
    1}\\s[a-zA-Z0-9\\s.#]+[;]{1}

Extract comments

<!--(.*?)-->

matching HTML label

<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>

Time regularization case

Simple date judgment (YYYY/MM/DD)

^\d{
    4}(\-|\/|\.)\d{
    1,2}\1\d{
    1,2}$ 

The date of evolution (YYYY/MM/DD| YY/MM/DD)

^(^(\d{
    4}|\d{
    2})(\-|\/|\.)\d{
    1,2}\3\d{
    1,2}$)|(^\d{
    4} year \d{
    1,2} month \d{
    1,2} Japan $)$ 

Add leap year judgment

example :

^((((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-))$ 

analysis :

What is a legal date range ? For different application scenarios , There are different explanations for this problem . Adopted here MSDN The agreement in :

DateTime Value class Type representation value Fan around In A.D. ( Christ Discipline element )0001 year 1 month 1 Midnight 12:00:00 A.D. (C.E.) 9999 year 12 month 31 On Tuesday night 11:59:59 Between date and time

The explanation of leap year .

The leap year of the Gregorian calendar is stipulated in this way : One revolution of the earth around the sun is called a regression year , One year old 365 Japan 5 when 48 branch 46 second . therefore , The Gregorian calendar stipulates that there are ordinary years and leap years , Every year there are 365 Japan , It's shorter than the year of return 0.2422 Japan , Four years is short 0.9688 Japan , So add one day every four years , This year there are 366 Japan , It's a leap year . But one more day in four years is more than four years of return 0.0312 Japan ,400 There will be more 3.12 Japan , Therefore, in 400 In the middle of the year 3 Leap years , That is to say 400 In the middle of the year, only 97 Leap years , In this way, the average length of the Gregorian calendar year is similar to that of the regression year . It is stipulated that : The year is a hundred, it must be 400 A leap year is a multiple of , for example 1900 year 、2100 A year is not a leap year .

First you need to verify the year , obviously , The year range is 0001 - 9999, matching YYYY Positive of be Expression for :

[0-9]{
    3}[1-9]|[0-9]{
    2}[1-9][0-9]{
    1}|[0-9]{
    1}[1-9][0-9]{
    2}|[1-9][0-9]{
    3}

among [0-9] It can also be expressed as \d, but \d Not as good as [0-9] intuitive , So I'll always use [0-9]

There are two difficulties in using regular expressions to verify dates : First, the number of days in big and small months is different , Second, leap year .

For the first difficulty , We don't think about leap years in the first place , hypothesis 2 Every month is 28 God , such , Month and date can be divided into three situations :

(1) Month is 1, 3, 5, 7, 8, 10, 12, The range of days is 01 - 31, matching MM-DD The regular expression of is :

(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])

(2) Month is 4, 6, 9, 11, The range of days is 01-30, matching MM-DD The regular expression of is :

(0[469]|11)-(0[1-9]|[12][0-9]|30)

(3) Month is 2, Consider the situation in ordinary years , matching MM-DD The regular expression of is :

02-(0[1-9]|[1][0-9]|2[0-8])

According to the above results , We can get the matching year date format as YYYY-MM-DD Regular expression of :

([0-9]{
    3}[1-9]|[0-9]{
    2}[1-9][0-9]{
    1}|[0-9]{
    1}[1-9][0-9]{
    2}|[1-9][0-9]{
    3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))

Now let's solve the second difficulty : Leap year considerations . According to the definition of leap year , We can divide leap years into two categories :

(1) Can be 4 Divide but not be 100 Divisible year . Look for the change of the last two figures , You can quickly get the following regular match :

([0-9]{
    2})(0[48]|[2468][048]|[13579][26])

(2) Can be 400 Divisible year . Can be 400 Divisible numbers are bound to be 100 to be divisible by , So the last two must be 00, We just need to make sure that the top two can be 4 Can be divided exactly by , The corresponding regular expression is :

(0[48]|[2468][048]|[3579][26])00 

Regular expression of the strongest validation date , Added leap year validation

The date format supported by this date regular expression is as follows .

YYYY-MM-DD 
YYYY/MM/DD 
YYYY_MM_DD 
YYYY.MM.DD

The complete regular expression is as follows

((^((1[8-9]\d{
    2})|([2-9]\d{
    3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{
    2})|([2-9]\d{
    3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{
    2})|([2-9]\d{
    3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))

Leap year's 2 Month has 29 God , So the matching leap year date format is YYYY-MM-DD The regular expression of is :

(([0-9]{
    2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29

Last , Merge the date validation expressions of normal year and leap year , We get the final validation date format as YYYY-MM-DD The regular expression of is :

(([0-9]{
    3}[1-9]|[0-9]{
    2}[1-9][0-9]{
    1}|[0-9]{
    1}[1-9][0-9]{
    2}|[1-9][0-9]{
    3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{
    2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)

DD/MM/YYYY The regular validation expression for the format is

(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{
    3}[1-9]|[0-9]{
    2}[1-9][0-9]{
    1}|[0-9]{
    1}[1-9][0-9]{
    2}|[1-9][0-9]{
    3}))|(29/02/(([0-9]{
    2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))

You can collect it first , Check out these common regular expressions !

Okay , That's all for today , I'm glacier , If you have any questions, please leave a message , You can also send me a private message on wechat , I'll get back to you when I see it , Last , Like it, guys 、 Looking at 、 Leaving a message. , forward , Let's go ~~

Okay , That's all for today , I'm glacier , See you next time ~~

At the end

If you want to enter a large factory , I want a promotion and a raise , Or I'm confused about my current job , You can communicate with me by private letter , I hope some of my experiences can help you ~~

Recommended reading :

Okay , That's all for today , Like it, guys 、 Collection 、 Comment on , Get up with one button three times in a row , I'm glacier , See you next time ~~

原网站

版权声明
本文为[glacier]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050034280124.html