Sinsing teaches you to quickly master the regular expression of PHP

Source: Internet
Author: User
Tags php define
First of all, this article is also I read a number of Daniel's blog after summing up, so first to these Daniel expressed high respect and gratitude, because of the number of people, and the source is particularly scattered, not one by one introduced, forgive me.

Cross-language theme **************

1. Just like XML, JSON, cookie, session, get, post, and so on, regular expressions are a topic that spans the language, and almost all languages support regular expressions, so that, as long as the language supports strings, it should support regular expressions. Because many features do not use regular expressions in particular, it is very easy to use regular expressions.

2. Regular expressions can be used for the verification of mailbox, QQ, mobile phone number and so on, but also can be used to collect Web pages, of course, the regular expression can be competent for most of the operation of the character text, of course, for parsing XML and other files, the regular expression is not very strong, However, regular expressions are still very useful for extracting specific textual information from XML.

3. Therefore, to learn the regular expression, regardless of the future transfer of which language, is very simple.


The function of regular expression **********

1. The main function of the regular expression is three, the first is a regular match, the second is a regular replacement, and the third is a regular extraction.

2. The so-called regular match, is to verify whether a string of text conforms to a regular expression rule, for example, we verify whether the user is a mailbox, the use of a regular match.

3. The so-called regular substitution is that we replace some of the information in the text with some other information, similar to the Find and replace function in Word editor, to quickly correct.

4. The so-called regular extraction, even if we extract the required information from the text according to our regular expression, for example, we extract all the tags from an HTML file, and then according to its SRC attribute to get all the images of hyperlinks, this is from a large string of text information to extract the information you will use.

5. We will first learn the writing of regular expressions and then learn how to use regular expression functions in PHP to manipulate regular expressions.


Rules for the writing of regular expressions--a ***************

1. Regular expressions are usually used as delimiters, that is, the portion between two slashes is treated as a regular expression rule, and we use it to match other text, such as/xinxing/, which means that only histograms string xinxing can be used.

2. As we have seen above, our characters are available in regular expressions, which are equivalent to one of the characters in the match, but the regular expression also provides some meta characters to make it easier for us to manipulate these characters, preceded by a character called a leading character, A leading character and a meta character work together.

3. We use + to indicate that its leading character appears consecutively or more times in the target, * indicates that its leading character appears 0 or more times in the target, while the? Specifies that its leading object must appear in the target object continuously 0 times or once, after which it is no more.

4. For example,/xin+/can be matched to xin,xinnnn,xinn,xinnnnn and so on, because here the + makes n can appear one or more times, and Xin? You can match Xinxing,xingui,xinguimeng characters, as long as there is Xin on the front.

5. Metacharacters are a very coarse way of specifying the number of times, we can take a more granular operation, such as {m,n} indicates at least m times, at most n times, {m,} indicates at least m times, {, n} indicates that there are at most n times, and {T} indicates a fixed T-time.

6. For example,/xin{3,4}/can match the two cases of xinnn and xinnnn.


Rules for the writing of regular expressions two *****************

1. Before we can use a single character to make a leading character, obviously it is very weak, we can have its boss version, where \s is used to match a single space character, including tab and newline characters, \s is used to match a single except for all the spaces, \d used to match the characters from 0 to 9, \ W is used to match alphanumeric underscores, and \w is used for all characters that do not match the \w, a single point number. Represents an apostrophe of all characters except newline. From the above data can be seen, \s and \s the meaning of the opposite, \w and \w is also the case.

2. For example,/\s+/can be used to match n spaces,/\d00/can match 000, 100, 200 and other hundred digits.


A locator and range representation in a regular expression *********

1. There is also the term "locator" in the regular expression, where ^ must appear at the beginning of the string, and $ must appear at the end of the string, while \b must be 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. It can be seen that ^ and $ are pairs of antonyms, while \b and \b are also a pair of antonyms.

3. We can enclose a single character in parentheses, and specify a range within it, such as/[a-z]/for lowercase letters from A to Z, such as/[0-9]/for lowercase letters from 0 to 9, such as/[a-c2-6]/ Indicates a to C or a letter or number from 2 to 6 to match.

4. This range modifier can be connected to a meta-character, such as/[a-z][0-9]+/, with one letter as the forerunner, followed by one or more numbers.

5. There is also a negative symbol ^ used in brackets is the exclusion, for example [^a-c] means to remove any letter other than ABC, when ^ in brackets, the negation, when it is outside the brackets, is considered as a locator.

6. We usually use \ to denote escape symbols, yes, in regular expressions we also need to escape with \ means, for example, \* is the symbol that matches *.


Parentheses *****************

1. As mentioned above, metacharacters can only act on their leading characters, and we can enclose several characters in parentheses as a whole, when metacharacters can be used for multiple characters.

2. For example A (BC) * indicates that an A is followed by 0 or more BC.

3. For example A (BC) {0,7} indicates that a followed by 0 to 7 BC can be.

4. Another | Indicates that the OR, that is, logical and, represents two of the selected one.

5. For example, Hi|hello matches hi or hello, for example (A|B) C means match AC or BC.


Little Practice ****************

1. Following our previous introduction, we have a little walkthrough here to see how much we have mastered.

2. In fact ab* and ab{0,} expression meaning is the same, AB? means match A or AB.



Remark *****************

1. Above we explained the most important part of the regular expression, but also some minor details are not scruples to, below I put more importantly to say again.

2. In front of us to use/as a delimiter, in fact, you can also use # as a delimiter, it is in the regular expression/appear more time to use, when the regular expression in the slash does not need to escape.

3. At the end of the regular expression, add an I to indicate that the casing is not case-sensitive.

4. We can group, also some call the group, that is, group, call format (? P <组名> ) call mode (? p= group name).


PHP function ****************

1. We use Preg_match to complete the regular matching function, its first parameter is a regular expression, the second parameter is to match the text, the third parameter is optional, to indicate the matching to the information, in fact, it has five parameters, but we often do not use so much, the following code example:

 2. The output from the above example is:
Array ([0] = = Array ([0] = XI [1] = XI [2] = = XI [3] = = XI))
3. We can use Preg_replace to do regular replacement, for example, we use the following code, all Xi is replaced for OO, code:

 4. Output: OONOOSGXAGOOAGSLGOOGSGHG 

5. Regular segmentation is to divide the original string into arrays by corresponding form, we use the Preg_split function, code example:

!--? php//use commas or spaces to separate the phrase $reg = "/[\s,]+/";//define the original string $msg = "Hypertext language, programming"; $ Keywords = preg_split ($reg, $msg);p Rint_r ($keywords);  6. Output: 

array ([0] = = Hypertext [1] = = Language [2] = = Progra mming) 



******************** summary **********************

1. Regular expressions are typically difficult to learn and easy to forget. Knowledge, hope can share with your readers.

2. We will brush up once 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.