Xin Xing teaches you how to quickly learn PHP regular expressions-php Tutorial

Source: Internet
Author: User
Xin Xing teaches you how to quickly master the regular expressions of PHP. First of all, this article is also summarized after I read several blogs of Daniel, therefore, I would like to express my high respect and gratitude to these Daniel. I will not discuss them one by one because of the large number of students and the scattered sources. sorry.

**************

1. like xml, json, cookie, session, get, post, and other topics, regular expressions are language-specific topics. almost all languages support regular expressions, as long as this language supports strings, it should support regular expressions, because many functions are especially difficult to use without regular expressions, but it is very simple to use regular expressions.

2. regular expressions can be used for Mailbox, QQ, mobile phone number verification, and can also be used to collect web pages. to put it bluntly, regular expressions can be used for most operations on character text. of course, for parsing files such as xml, the regular expression is not very powerful, but the regular expression is still very powerful for extracting specific text information in xml.

3. Therefore, it is very easy to learn regular expressions, no matter which language you will switch to in the future.


* *********** The role of a regular expression **********

1. there are three main functions of regular expressions. The first is regular expression matching, the second is regular expression replacement, and the third is regular expression extraction.

2. the so-called regular expression matching is used to verify whether a string of text conforms to a regular expression rule. for example, we use regular expression matching to verify whether the user's email address is a mailbox.

3. the so-called regular expression replacement is to replace some information in the text with other information, similar to the search and replacement function in the word editor, which can be used to quickly correct errors.

4. the so-called regular expression extraction, even if we extract the required information from the text according to our regular expression, for example, we extract all tags from an html file, then, you can obtain the hyperlinks of all images based on its src attribute. this is to extract the information you will use from a large string of text information.

5. First, we will learn how to write regular expressions, and then learn how to use regular expression functions in PHP to operate regular expressions.


* ************* Regular expression writing rule 1 ***************

1. regular expressions usually use "/" as the delimiter, that is, the part between two diagonal lines is used as the regular expression rule. We use it to match other texts, for example, the/xinxing/regular expression indicates that only the matching string xinxing can be used.

2. as we can see above, our characters can appear in regular expressions, and their functions are equivalent to matching a character. However, regular expressions also provide metacharacters to facilitate operations on these characters. these metacharacters are preceded by a character called a leading character, a leading character, and a metacharacters.

3. we use + to indicate that its leading character appears one or more times consecutively in the target. * indicates that its leading character appears zero or multiple times in the target? It is required that its leading object must appear zero times or once consecutively in the target object, and the subsequent objects will not work.

4. for example,/xin +/can match with xin, xinnnn, xinn, and xinnnnn, because the + here allows n to appear once or multiple times, while xin? It can match characters such as xinxing, xingui, and xinguimeng, as long as there is a xin in front of it.

5. metacharacters are a rough way to specify the number of times. we can perform more fine operations, for example, {m, n} indicates that at least m appears, at most n times, {m,} indicates that at least m appears, {, n} indicates that at most n appears, and {t} indicates that a fixed t appears.

6. for example,/xin {3, 4}/can match xinnn and xinnnn.


* *********** Regular expression writing rule 2 *****************

1. we can use a single character as the leading character. obviously this is very weak. we can have its boss version, where \ s is used to match a single space character, including tabs and line breaks, \ S is used to match all characters except spaces, \ d is used to match characters from 0 to 9, and \ w is used to match letters, numbers, and underscores, \ W is used for all characters that do not match \ w. It is a separate point number. indicates all characters except line breaks. From the data above, we can see that \ s and \ S have the opposite meaning, and \ w and \ W are the same.

2. for example,/\ s +/can be used to match n spaces and/\ d00/can be used to match hundreds of numbers such as 000, 100, and 200.


************** The positioning operator and range in the regular expression *********

1. the regular expression also has the so-called locator, where ^ must appear at the beginning of the string, and $ must appear at the end of the string, while \ B must appear at the beginning or end of the target string, and \ B must not appear at the beginning or end, but only in the middle.

2. we can see that ^ and $ are antonyms, while \ B and \ B are also antonyms.

3. we can enclose a character in brackets and specify a range in it. for example,/[a-z]/indicates that lowercase letters are from a to z, for example,/[0-9]/indicates a number with lowercase letters ranging from 0 to 9, for example,/[a-c2-6]/indicates that a letter or number from a to c or from 2 to 6 matches.

4. this range modifier can be connected to metacharacters. for example,/[a-z] [0-9] +/indicates that a letter is used as the forerunner and then followed by one or more numbers.

5. also, the negative sign ^ indicates exclusion in brackets. for example, [^ a-c] indicates any letter except abc. when ^ is in brackets, it indicates no, when it is out of brackets, it is regarded as a positioning character.

6. we usually use \ to indicate the escape symbol. that's right. in a regular expression, we also need to use \ to indicate the escape, for example, \ * to indicate matching.


*****************

1. as mentioned above, metacharacters can only act on leading characters. we can enclose several characters in parentheses as a whole. in this case, metacharacters can act on multiple characters.

2. for example, a (bc) * indicates that one a is followed by zero or multiple bc.

3. for example, a (bc) {0, 7} indicates that a can be followed by 0 to 7 bc.

4. another | represents OR, that is, logical and, indicating that one of the two is selected.

5. for example, hi | hello indicates matching hi or hello. for example, (a | B) c indicates matching ac or bc.


****************

1. after our previous introduction, we will conduct a small drill here to see how much we have mastered.

2. in fact, the expression of AB * and AB {0,} is the same, AB? Match a or AB.



*****************

1. we have explained the most important part of the regular expression above, but I have no scruples about some details. let's talk about the important part here.

2. as we have mentioned above, we can use '/' as the separator. In fact, we can also use '#' as the separator, which is used when there are many regular expressions, in this case, the diagonal lines in the regular expression do not need to be escaped.

3. add an I at the end of the regular expression to indicate case insensitive.

4. we can Group, or someone calls it a Group. The Call Format is (? P <组名> ) Call method (? P = group name ).


****************

1. we use preg_match to complete the regular expression matching function. Its first parameter is a regular expression, the second parameter is the text to be matched, and the third parameter is optional, it indicates the matched information. In fact, it has five parameters, but we often cannot use that much. the following code example:

 2. the output result of the above example is:
Array ( [0] => Array ( [0] => xi [1] => xi [2] => xi [3] => xi ) )
3. we can use preg_replace for regular expression Replacement. for example, we can use the following code to replace all xi values with oo values. the code is as follows:

  4. output result: oonoosgxagooagslgoogsghg

5. Regular expression Segmentation: splits the original string into arrays in the corresponding form. We use the preg_split function. sample code:

   6. output result:

Array ( [0] => hypertext [1] => language [2] => programming )



******************* ***

1. Regular expressions are a typical hard-to-learn and easy-to-forget knowledge. I hope to share with you.

2. we review it every time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.