Hello, I'm Glacier~~
I spent two days sorting out these tasks with regular expressions that I often use. My little friends take them away. Thank you.
This time I shared the regular expressions that I often used in my work. It is precisely these regular expressions that I have mastered. Ice River writes an average of 200 lines less code per day than others, which greatly improves the efficiency of research and development. I recommend that small partners collect them and try to use them in their own projects in the ordinary time!!
A good command of regular expressions can help programmers write the most elegant code at the fastest speed.
Ice River has been programming for many years. It has combed and summarized the regular expressions that have been used. 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, Ice River has opened its regular expressions to its little partners, which it often uses, in the hope that they can get substantial help.
Ice River Common Regular
Integer or decimal
^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers can be entered
^[0-9]*$
You can only enter n-bit numbers
^d{n}$
You can only enter numbers with at least n digits
^d{n,}$
You can only enter m~n digits
^d{m,n}$
You can only enter numbers that start with zero or non-zero
^(0|[1-9][0-9]*)$
Only positive real numbers with two decimals can be entered
^[0-9]+(.[0-9]{2})?$
Only positive real numbers with 1-3 decimal places can be entered
^[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*$
Only characters of length 3 can be entered
^.{3}$
Only strings of 26 letters can be entered
^[A-Za-z]+$
Only strings of 26 uppercase letters can be entered
^[A-Z]+$
Only strings of 26 lowercase letters can be entered
^[a-z]+$
Only strings consisting of numbers and 26 letters can be entered
^[A-Za-z0-9]+$
Only strings consisting of numbers, 26 English letters, or underscores can be entered
^w+$
Verify user password:
^[a-zA-Z]w{5,17}$
Note: The correct format is: start with a letter, 6-18 in length, and can only contain characters, numbers and underscores.
Verify that ^%&,;=?$ Equal Character
[^%&',;=?$x22]+
Only Chinese characters can be entered
^[u4e00-u9fa5]{0,}$
Verify Email Address
^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$
Verify 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-XXX XXX, XXX-XXXXXXX, XXX-XXXXXXX XXX, XXX-XXXXXXXX, XXXXXXX and XXXXXXXX
Authentication ID number (15 or 18 digits)
^d{15}|d{18}$
Validate 12 months of a year
^(0?[1-9]|1[0-2])$
The correct formats are: 01-09 and 1-12
Verify 31 days of a month
^((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 expression matching empty lines
n[s| ]*r
Regular expressions matching html tags
<(.*)>(.*)</(.*)>|<(.*)/>
Regular expression matching first and last spaces
(^s*)|(s*$)
A regular expression matching an Email address
w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Regular expressions matching HTML Tags
<(S*?)[^>]*>.*?|<.*? />
Note: The version that is streaming online is too bad, and this one matches only part of it, and there is nothing you can do about complex nested tags.
Regular expression matching white space at beginning and end
^s*|s*$
Comment: Useful expressions that can be used to delete blank characters (including spaces, tabs, page breaks, etc.) at the beginning and end of a line
A regular expression matching an Email address
w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Comment: Useful for form validation
Regular expression matching URL of web address
[a-zA-z]+://[^s]*
Note: Versions that are streamed online have limited functionality, and this is basically what you need.
Is the matching account number legal (letters start, allow 5-16 bytes, allow alphanumeric underscores)
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: Useful for form validation
Match domestic phone number
d{3}-d{8}|d{4}-d{7}
Comment: Match form such as 0511-4405222 or 021-87888822
Match Tencent QQ
[1-9][0-9]{4,}
Comment: Tencent QQ starts from 10000
Match China Postal Code
[1-9]d{5}(?!d)
Comment: China Postal Code is 6 digits
Match ID
d{15}|d{18}
Note: China has 15 or 18 ID cards
Match ip address
d+.d+.d+.d+
Note: Useful for extracting ip addresses
Match a specific number
^[1-9]d*$ //Match positive integer ^-[1-9]d*$ //Match Negative Integer ^-?[1-9]d*$ //Match Integer ^[1-9]d*|0$ //Match nonnegative integers (positive integer + 0) ^-[1-9]d*|0$ //Match nonpositive integers (negative integer + 0) ^[1-9]d*.d*|0.d*[1-9]d*$ //Match positive floating point numbers ^-([1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point numbers ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //Matching floating point numbers ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //Match nonnegative floating point numbers (positive + 0) ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$//Matching non-positive floating point numbers (negative floating point + 0)s
Note: Useful when dealing with large amounts of data, pay attention to corrections when applying.
Match a specific string
^[A-Za-z]+$//Matches a string of 26 letters ^[A-Z]+$//Matches a string of 26 uppercase letters ^[a-z]+$//Matches a string of 26 lower case letters ^[A-Za-z0-9]+$//Match strings consisting of numbers and 26 English letters ^w+$//Matches a string of numbers, 26 English letters, or underscores
Note: Some of the most basic and common expressions
Check password strength such as password strength: contains a combination of upper and lower case letters and numbers, cannot use special characters, length is between 8-10.
^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
Check String
Chinese.
^[u4e00-u9fa5]{0,}$
A string of numbers, 26 English letters, or underscores
^w+$
Verify E-Mail address
[w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?
Verify ID number 15 digits:
^[1-9]d{7}((0\d)|(1[0-2]))(([0|1|2]d)|3[0-1])d{3}$
18 bits:
^[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)$
Date validation in the format yyyy-mm-dd, with leap year 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)$
Validate amount to 2 decimal places.
^[0-9]+(.[0-9]{2})?$
Below the check mobile number are regular expressions for mobile numbers starting at 13, 15 and 18 in China. (The first two digits starting number 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}$
Determine the version of IE
^.*MSIE [5-8](?:.[0-9]+)?(?!.*Trident/[5-9].0).*$
Verify 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
Verify 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 prefixes of URL s
In application development, it is often necessary to distinguish whether a request is HTTPS or HTTP. The following expression allows you to prefix a url and make logical decisions.
if (!s.match(/^[a-zA-Z]+:///)) { s = 'http://' + s; }
Extract URL Links
This expression below filters out URL s in a piece of text.
^(f|ht){1}(tp|tps)://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
File Path and Extension Checks validate file paths and extensions under windows (in the following example, a.txt file)
^([a-zA-Z]:|)([^]+)*[^/:*?"<>|]+.txt(l)?$
Extracting the color code of a Web page sometimes requires extracting the color code of a Web page. You can use the following expression.
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Extract pictures from web pages
< *[img][^>]*[src] *= *["\']{0,1}([^\"' >]*)
Extract Page Hyperlinks
(<as*(?!.*brel=)[^>]*)(href="https?:\/\/)((?!(?:(?:www\.)?'.implode('|(?:www\.)?', $follow_list).'))[^"]+)"((?!.*brel=)[^>]*)(?:[^>]*)>
Find CSS Properties
^s*[a-zA-Z-]+s*[:]{1}s[a-zA-Z0-9s.#]+[;]{1}
Extract Comments
<!--(.*?)-->
Match HTML Tags
</?w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+s*|s*)/?>
Time Regular Case
Simple date judgment (YYYY/MM/DD)
^d{4}(-|/|.)d{1,2}1d{1,2}$
Date Judgment of Evolution (YYYY/MM/DD| YY/MM/DD)
^(^(d{4}|d{2})(-|/|.)d{1,2}3d{1,2}$)|(^d{4}year d{1,2}month d{1,2}day $)$
Judgment of leap year addition
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]|1d|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 the legal date range? This question has different interpretations for different scenarios. This follows the conventions in MSDN:
The DateTime value type represents the date and time between 12:00:00 p.m. and 11:59:59 p.m. on January 1, 2001 and A.D. (C.E.) on December 31, 1999
Interpretation of leap years.
As for leap years in the Gregorian calendar, it is stipulated that one cycle of the earth's rotation around the sun is called a regression year, with a regression age of 365 days, 5:48 minutes and 46 seconds. Therefore, the Gregorian calendar stipulates that there are ordinary years and leap years. The average year has 365 days, which is 0.2422 days shorter than the year of return and 0.9688 days shorter than the year of return. Therefore, it increases by one day every four years. This year has 366 days, which is the leap year. However, four years with one day more than four regression years will be 0.0312 days more, and after 400 years there will be 3.12 days more. Therefore, there will be three less leap years in 400 years, that is, 97 leap years in 400 years, so the average length of Gregorian calendar years will be similar to that of regression years. It is thus stipulated that a leap year is only a multiple of 400 if the year is an integer hundred, for example 1900 and 2100 are not leap years.
First you need to verify the year. Obviously, the year range is 0001 - 9999, and the regular expression matching YYYY 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}
Where [0-9] can also be expressed as d, but D is not as intuitive as [0-9], so I'll keep using [0-9]
There are two difficulties in validating dates with regular expressions: first, the number of days in different months, and second, leap year considerations.
For the first difficulty, let's not consider leap years first. Assuming that February is 28 days, month and date can be divided into three cases:
(1) Month is 1, 3, 5, 7, 8, 10, 12, day range 01 - 31, regular expression matching MM-DD is:
(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])
(2) Months are 4, 6, 9, 11, days range from 01-30, and the regular expression matching MM-DD is:
(0[469]|11)-(0[1-9]|[12][0-9]|30)
(3) Month is 2, and the regular expression matching MM-DD is:
02-(0[1-9]|[1][0-9]|2[0-8])
Based on the above results, we can get regular expressions that match the flat year date format of YYYY-MM-DD:
([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])))
Next, we address the second difficulty: leap year considerations. According to the definition of leap years, leap years can be divided into two categories:
(1) Year that can be divided by 4 but not by 100. Looking for the latter two changes, you can quickly get the following regular matches:
([0-9]{2})(0[48]|[2468][048]|[13579][26])
(2) The year divisible by 400. The number that can be divided by 400 must be divided by 100, so the last two must be 00. We just need to make sure that the first two can be divided by 4. The corresponding regular expression is:
(0[48]|[2468][048]|[3579][26])00
Regular expression for the strongest validation date, with leap year validation added
The date format supported by this date regular expression is shown below.
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)$))
February has 29 days for leap year, so the regular expression matching leap year dates in YYYY-MM-DD format is:
(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29
Finally, by combining the date validation expressions for flat and leap years, we find that the final validation date format is YYYY-MM-DD:
(([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)
The regular validation expression for DD/MM/YYYY* 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)))
Partners can collect first and then check out these common regular expressions!
Okay, let's get here today. I'm Ice River. You can leave a message if you have any questions, or you can trust me on WeChat. I will reply to you when I see it. Finally, the little buddies say yes, look at it, leave a message, forward it, and get up ~~
Write at the end
If you want to enter a big factory, want to get a promotion and pay raise, or are confused about your existing job, you can trust me to communicate with you. I hope some of my experiences can help you ~~
Recommended reading:
- <Practice tells the truth: the strongest secondkill system architecture in the whole network decrypts, not all secondkill are secondkill!!>
- <From zero to hundreds of millions of users, how do I step by step optimize my MySQL database? (recommended collection)>
- <I further optimized the massive data proofreading system with multi-threading under the billion-level traffic e-commerce business, and the performance improved by 200%!! (Full dry, recommended collection)>
- <I used multi-threading to optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance directly improved by 200%!! (Full dry, recommended collection)>
- <I sum up the best learning route for concurrent programming with 10 diagrams!! (recommended collection)>
- <In high concurrency scenarios, a faster lock than a read-write lock, I'm completely convinced!! (recommended collection)>
- <Summary of the best performance optimization of the whole network!! (Glacier hematemesis, recommended collection)>
- <MyBatis is over in three days, so feel free to ask!! (Glacier hematemesis, recommended collection)>
- <Advise those new students and sisters who have just joined the work: to enter the factory, you must master these concurrent programming knowledge! Complete Learning Route!! (recommended collection)>
- <Advise those students and sisters who have just joined the job: these core skills are what you must master in order to enter a big factory! Complete Learning Route!! (recommended collection)>
- <Advise those new students and sisters: the sooner you know the fundamentals of these computers and operating systems, the better! Wan Zi Long Text is too top!! (recommended collection)>
- <I spent three days developing a national game for all ages, supporting music playback and now opening the full source code and notes (recommended collection)!!>
- <I am a high concurrent programmer with the hardest core of the whole network and the most notable blogger of CSDN. Do you agree? (recommended collection)>
- <Five years after graduation, from 3000 to millions a month, what core skills have I mastered? (recommended collection)>
- <I invaded Wifi of my next sister and found out... (Full-range dry goods, recommended collection)>
- <Don't try panda fragrance easily. No, I regret it!>
- <During the Qingming Festival, I secretly trained Pandas to burn incense. As a result, my computer "dedicated" to pandas!>
- <The 73,000 word Liver Explosion Java8 new feature, I don't believe you can see it! (recommended collection)>
- <What is the experience of unplugging a server during peak business hours?>
- <Summary of the most Linux commands on the whole network!! (most complete in history, recommended collection)>
- <Write a tool in Python that cracks MySQL perfectly!! (recommended collection)>
- <What on earth is the SimpleDateFormat class not thread safe? (with six solutions, recommended collection)>
- <These three new indexes in MySQL 8 take MySQL off directly, you don't even know!! (recommended collection)>
- <After completing Spring source, I open source this distributed caching framework!! (recommended collection)>
- <Billion-level traffic and concurrent seconds killing system merchandise are "oversold", only because there are two huge pits in the JDK synchronization container used!! (True record of trampling pits, recommended collection)>
- <Advise those new students and sisters to learn concurrent programming well, you must pay attention to the pits of these concurrent containers!! (recommended collection)>
- <Company's reporting tools are too difficult to use, I have three days of working with an Excel tool, Miss Operations call is too good, now open source!! (recommended collection)>
- <Advise those students and sisters who have just joined the work: these core concurrent programming skills are what you must master if you want to enter a big factory!! (recommended collection)>
- <Ali Interviewer: How does the high concurrent and high flow secondhand killing system correctly solve the inventory oversold problem? (recommended collection)>
- <Redis five data types and usage scenarios summary!! (with full combat cases, recommended collection)>
Okay, let's get here today. My little buddies like praises, collections, comments and one click at a time. I'm glacier. I'll see you next time~~
??? Search for the public number below to follow me???