Know this 20 regular expressions, it can make you less write 1,000 lines of code
HTTPS: // zhuanlan.zhihu.com/p/29163295
been said to learning Regular never learn ..
Regular expressions, a very old and powerful text processing tools, in just a very brief expression statement, will be able to quickly implement a very complex business logic. Mastered regular expressions, you can make your development efficiency greatly improved.
Regular expressions are often used to check or any string fields, such as date formats substantially below this check JavaScript code:
var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/;
var r = fieldValue.match(reg);
if(r==null)alert('Date format error!');
The following is a technical Carpenter finishing, frequently used in front-end development in 20 regular expressions.
1. Verification password strength
Strength of the password must contain a combination of uppercase and lowercase letters and numbers, special characters can not be used, a length of between 8-10.
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
2. Verify Chinese
String only is Chinese.
^[\\u4e00-\\u9fa5]{0,}$
3. The string of digits, letters or underscore 26 composed of
^\\w+$
4. Verify E-Mail Address
Like password, below is the E-mail address regular compliance checks statement.
[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
The ID number verification
The following is a regular check identity card number. 15 or 18.
15位:
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$
18位:
^[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)$
6. Verify date
"Yyyy-mm-dd" format check date has been considered flat leap years.
^(?:(?!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)$
7. check amount
Check amount, accurate to two decimal places.
^[0-9]+(.[0-9]{2})?$
8. check phone number
Here is the phone number 13, 15 at the beginning of the regular expression. (Can be extended according to the first two numbers at the beginning of 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}$
IE version 9. Analyzing
IE yet to be completely replaced, still need to do a lot of pages compatible version, the following expression is IE version check.
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$
10. Verify IP-v4 address
IP4 regular statements.
\\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
11. The IP-v6 address check
IP6 regular statements.
(([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]))
12. Check the URL prefix
Application development is often necessary to distinguish between requests HTTPS or HTTP, a prefix may be removed and then the url logical judgment by the following expression.
if (!s.match(/^[a-zA-Z]+:\\/\\//))
{
s = 'http://' + s;
}
13. The extract URL link
The following expression can be selected piece of text in the URL.
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?
14. The path and file name extensions check
Verify the path and file name extension under Windows (the example below as a .txt file)
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
15. 提取Color Hex Codes
Sometimes the color code needs to be extracted a Web page, the following expression may be used.
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
16. The extract Web Images
If you want to extract all the image info pages, you can use the following expression.
\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)
17. The extract page hyperlinks
Extract hyperlinks in html.
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
18. Find a CSS property
By the following expression, you can search to match the CSS properties.
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
19. Notes extraction
If you need to remove the comment in HMTL, you can use the following expression.
<!--(.*?)-->
20. Match HTML tags
Attribute of an HTML tag can be matched by the following expression.
<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>
Regular expression syntax-related
Here is a very good I found regular expression Cheat Sheet, it can be used to quickly find the relevant syntax.
Learn the regular expression
I read an article quite good regular expressions quick study guide , interested students can continue in-depth study of reference.
Regular Expressions online testing tool
regex101 is a very good regular expression online testing tool, you can go directly online to test your regular expressions oh.
In addition, I also found a few good online regex aspects of tutorials and books, and to share their skills Carpenter agency jijiangshe.com , if you are interested in learning Welcome to the acquisition. ^ _ ^
Author: Carpenter Technology
http://www.jianshu.com/p/e7bb97218946
——————————————————————————————————————
Do you want to learn more in-depth understanding of Python knowledge, you can look at our finishing more than a month spent hundreds of hundreds of hours of content knowledge systems of: