Basic PHP development tutorial: Tips for writing regular rules
1. Tips for writing regular rules
Tips for regular rules: write a little and test a little.
Because we need constant regularization and use preg_match to compare whether the match can be successful. If it succeeds, let’s write the next point. Until you finish writing and all matches are successful!
For example, I want to write a regular expression for an email address. The first thing I want to do is list all the commonly used email formats. For example:
liwenkai@phpxy.com liwenkai@corp.baidu.cm liwenkai@126.com l_w_k@xxx.com 12345@qq.com
The commonly used formats mainly include the following. Then we can analyze:
1. First match the character before @ \w+ (because it is 0-9A-Za-z_)
2 . The second one is followed by an @ symbol
3. The third one is written [a-zA-Z0-9-]+ because the main domain names of qq and 126 cannot have underscores. ’s
4. corp.baidu. Or 126. Usually the email suffix is like this. So we can write it as: ([a-zA-Z0-9-]+.){1,2}
##5. The above is to escape . so that it is itself the meaning of. The brackets must be repeated at least once and at most twice.
6. Just follow com|cn|org|gov.cn|net|edu.cn and so on
Therefore, our regular Expression usage: /\w+@([a-zA-Z0-9-]+.){1,2}(com|cn|org|gov.cn|net|edu.cn) /The regular expression of the mailbox is successful.2. Commonly used regular functions
Our commonly used regular functions are:3. Several regular expressions that are often tested in interviews are
1. Match email 2. Match mobile phone number3. Match a URL4. Use regular expression to match a certain format and take out a certain example5. Write a collector
Others....
Just search and copy if needed!