Regular Expressions abuse me a thousand times, I want to resist, resist
Scenario 1: Verify that email is valid
There are too many mailbox types, and what are the net suffixes? In special cases, we will not handle them. Below I have found some frequently used mailboxes:
Hyy@gmail.com Google Mail hyy12@qq.com qqmail hyy-123@163.com 163 mailbox 732662@sina.com Sina mail hyy@sohu.com Sohu mailbox hyy@hotmail.com hotmai mailbox hyy@189.cn 189 mailbox hyy@139.com 139 mailbox
1. Analysis Rules
Xxx Prefix: can be a combination of numbers, letters, and-@: This single character can be matched with a suffix: English or numbers.: dot separator ending: com or cn
2. match from the first rule
A prefix can be a combination of numbers, letters, and hyphens (-). A prefix can only be written in the middle of a number or letter and cannot be placed on both sides of the ending scene.
W + -? W + // indicates a number or letter, which can be inserted in the middle -.
Matching result, bold
Hyy@ Gmail.com Google Mail
Hyy12@ Qq.com QQ mailbox
Hyy-123@ 163.com 163 email
732662@ Sina.com Sina mail
Hyy@ Sohu.com Sohu email
Hyy@ Hotmail.com hotmai email
Hyy@ 189.cn 189 email
Hyy@ 139.com 139 email
3. Match @ characters
w+-?w+@
Matching result, bold
Hyy @Gmail.com Google Mail
Hyy12 @Qq.com QQ mailbox
Hyy-123 @163. com 163 email
732662 @Sina.com Sina mail
Hyy @Sohu.com Sohu email
Hyy @Hotmail.com hotmai email
Hyy @189. cn 189 email
Hyy @139. com 139 email
4. The subsequent rules are simple.
/w+-?w+@w+.(com|cn)/g
View matching results
Scenario 2: Verify the time format
There are many time formats. If you want to write a large regular expression to match all, it is very difficult. Below we only match xxxx-xx.
2017-09-15
1. Analysis Rules
It can be divided into several points: year, month, and day
Year: four digits, starting with 1 or 2
Month: 2 digits, starting with 0 or 1
Day: when the first digit is 1-9 and the second digit is 2, the first digit is 0-3, and the second digit is 0-9.
2. Matching year
Date regularization is one of the most difficult to write. It is very difficult to write precisely.
The first digit, 1 or 2, uses (1 | 2) {1}, 2-4 digits is 0-9, and is written as [0-9] {3}. The combination is year.
/(1|2){1}[0-9]{3}/g
3. Matching month
The connector between the year and month is written-
/(1|2){1}[0-9]{3}-/g
The composition of a month can be divided into two types: the beginning of 0 and the beginning of 1. If it is the beginning of 0, the first 0 may not exist, and the second is 0-9. If it starts with 1 and the second digit is 0-2, the combination is (0? [1-9] | 1 [0-2])
/(1|2){1}[0-9]{3}-(0?[1-9]|1[0-2])/g
4. Matching day
When the first digit of a date is 0, the second digit is 0-9, and the first digit is 1, the second digit is 0-9, and the first digit is 2, the second digit is 0-9, when the first digit is 3, the second digit is 0-1, then (0 [1-9] | 1 [0-9] | 2 [0-9] | 3 [0-1])
/(1|2){1}[0-9]{3}-(0?[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])/g
5. There are many date formats. You should write rules according to your work needs. Here I will list so many combinations of rules to practice.
Scenario 3: Verify that the URL is valid
Url regularization is also a complex scenario. I found several different URLs to test
https://segmentfault.com/writehttps://shimo.im/doc/ME90WXr4Hm8nx3Jl?r=PPGD2Dhttps://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching_FAQhttps://regex101.com/http://www.baidu.com/#/abouthttp://www.baidu.com
This time, we omitted the steps and analyzed them directly. It started with two methods: http: // and https ://
/https?:///g
The domain name may be xx.com or yy.xx.com.
/https?://w+.w+(.w+)?/?/g
At the end of the page, the rules become very complex, and I do not know all the writing methods after the url domain name. From the test set I wrote at the beginning, I can write the following form.
/https?://w+.w+(.w+)?/?([w-_#/?=.]+)?/g
This regular expression matches the test sample, but it is flawed. I also read some other statements on the Internet, and there are problems after the test, you still need to work hard to write a url regular expression with high precision.
Scenario 4: Obtain url parameters
Regular Expressions on the Internet Use window. location. search to obtain the parameter characters after the question mark. I use a new method that is pure. The url is optional.
Function getUrlParamName (name, url) {if (typeof name! = 'String') throw Error ('must be a string') if (! Url) {// if no url is uploaded, read the url of the current website. url = window. location. href} // match the array of name = value let arr = url. match (/(?! (? :(? | &) (W +) = (w +)/g); for (let v of arr) {// If an element matches a string composed of input parameters, returns the value. If (new RegExp (name + '= ([0-9a-zA-Z] +)', 'G '). test (v) {return v. match (new RegExp (name + '= ([0-9a-zA-Z] +)', 'I') [1]} return null} let t = getUrlParamName ('R ') console. log (t) // PPGD2D
You can take the time to study the method. Here I will teach you a new knowledge called assertion with zero width.
(?!(?:(?|&)))(w+)=(w+)
The syntax used here is as follows: it is divided into two parentheses (?!) on the left (?! (? :(? | &) Indicates matching? Or, but does not get the two symbols, but obtains the regular expression matching with it.
S = "? R = abc & qId = 123 "(?! (Regular) // The matching result is r = abc, qId = 123.
I have not yet mastered the zero-width assertion. It generally means to obtain the regular or followed by a character or a regular character before or after a certain character. It's not very tall. It's probably someone's translation, so he got a name.
Summary
This chapter shares the practices of four scenarios. I am not a regular expression guru, but I have just started to study regular expressions. The most important thing to learn is to know the rules currently required, "No rules and no regular expressions", there are still many symbols to remember for regular expressions. In the next step, I will practice more regular expressions and then look at the basic documents to consolidate the knowledge.
Good regular expressions can be used to write expressions with excellent performance. Regular Expressions are the same as those in js, and different writing methods may have performance gaps. When the data size is small, you can ignore it. When analyzing long texts, you need to become an excellent regular expression.
Author: February
Original article: https://segmentfault.com/a/1190000011194709
Disclaimer: The copyright of the article belongs to the author. If there is any infringement, please contact the editor to delete it.
Thank you for your message.
- This article has been included in the following columns: