Tips and common...LOGIN

Tips and commonly used regular expressions for writing regular expressions in PHP

Tips for writing regular rules

The trick for girls and boys when they are in love is usually to test whether you are nicer to me. If so, our relationship will be better.

The secret of regularity is basically the same as the secret of love: 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:

phpcn@php.cn phpcn@corp.baidu.cm phpcn@126.com phpcn@xxx.com 12345@qq.com

The commonly used formats are mainly as follows. Then we can analyze:

1. First match the character \w+ before @ (because it is 0-9A-Za-z_)

2. The second one is followed by an @ symbol

3. Write [a-zA-Z0-9-]+ for the third one because the main domain names such as qq and 126 cannot have underscores

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 has its own meaning. The brackets must be repeated at least once and at most twice.

6. Just follow com|cn|org|gov.cn|net|edu.cn, etc.

Therefore, our regular expression is shown in what I use:

/\w+@([a-zA-Z0-9-]+.){1,2}(com|cn|org|gov.cn|net|edu.cn)/

I successfully wrote the regular expression for the email address.

Commonly used regular functions

Our commonly used regular functions are:

function NameFunction
preg_filterPerforms a regular expression search and replace
preg_grepReturns the array entries matching the pattern
preg_matchPerforms a regular expression match
preg_match_allPerform a global regular expression match
preg_replace_callback_array Pass in an array and perform a regular expression search and replace using the callback
preg_replace_callbackPerforms a regular expression search and replaces using a callback
preg_replace Perform a regular expression search and replace
preg_splitSeparate strings by a regular expression

Please use the manual for these functions. If you have any questions or encounter problems, you can come to our official website to ask questions.

Regarding regular questions frequently encountered in interviews

Some regular questions frequently asked in interviews are:

1. Match email address

2. Match mobile phone number

3. Match a website address

4. Use regular expression to match a certain format and take out an example

5Write a collector
Others....

I am not worried about questions 4 and 5 of the interview, because as long as you carefully study the first five sections I gave content. Just reasoning for questions 4 and 5.

Because, usually during the technical question answering interview session, it is time to check the phone!

Commonly used regular expressions

The following is a quick search of regular expressions. You must know the specific meaning.
Just copy it when needed:

Next Section
<?php $pattern = '/(\w+)@(\w+).(com|org)/'; $str = "bob@example.com"; preg_match($pattern, $str, $match); print_r($match); ?>
submitReset Code
ChapterCourseware