Regular Expression learning 2 (common functions)

Source: Internet
Author: User

I went to the Internet to perform a search and finally understood it ~ \ (Too many rows )/~ La la

<? Php // preg_match () // int preg_match (string pattern, string subject [, array matches [, int flags]) // search for the content that matches the regular expression given by pattern in the subject string. // If matches is provided, it will be filled with the search results. $ Matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on. // Preg_match () returns the number of times that pattern matches. Either 0 (no match) or 1 time, because preg_match () will stop searching after the first match. Preg_match_all () indicates that, on the contrary, the end of the subject is always searched. If an error occurs in preg_match (), FALSE is returned. /// Preg_match_all () // int preg_match_all (string pattern, string subject, array matches [, int flags]) // search for all the content that matches the regular expression given by pattern in subject and put the result in matches in the order specified by flags. // After the first match is found, the next search starts from the end of the previous match. // Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit]) // search for matching items in pattern mode in subject and replace them with replacement. If limit is specified, only limit matches are replaced. If limit is omitted or its value is-1, all matches are replaced. // Replacement can contain reverse references in the \ n format or (from PHP 4.0.4) $ n format. The latter is preferred. Each such reference will be replaced with the text that matches the child pattern in the nth captured parentheses. N can be from 0 to 99, where \ 0 or $0 indicates the text matched by the entire mode. Count left parentheses from left to right (starting from 1) to obtain the number of child modes. // When the replacement mode is followed by a number after a reverse reference (that is, the number next to a matching mode ), the familiar \ 1 Symbol cannot be used to represent reverse references. For example, \ 11 will make preg_replace () confused that the reverse reference of \ 1 is followed by a number 1 or a reverse reference of \ 11. In this example, \ $ {1} 1 is used. This will form an isolated $1 reverse reference, and make the other 1 just plain text. // 1. replace $ str = "daoyu shi ge hao hai zi 5555"; $ pattern = "/\ s /"; // If the variable is defined as $ pattern_1, an error occurs. $ str = preg_replace ($ pattern, '-', $ str); echo $ str. "<br>"; // daoyu-shi-ge-hao-hai-zi-5555 // 2. reverse string reference // method 1 $ pat = "/(\ w +)-(\ w +) -(\ w +)-(\ d +)/I "; $ str = preg_replace ($ pat," \ $1 ", $ str ); echo $ str. "<br>"; // Note: if the following form is used, you will find that the matching result is zi-. Therefore, you can consider it as {6} with the number of times, he ($1) matches the last $ str = 'daoyu-shi-ge-hao-ha I-zi-5555 '; $ pat = "/(\ w +)-) {6} (\ d +)/I"; $ str = preg_replace ($ pat, "\ $1", $ str); echo $ str. "<br>"; // method 2 $ str = "daoyu-shi-ge-hao-hai-zi-5555"; $ pat = "/(\ w +)-(\ w +) -(\ w +)-(\ d +)/I "; $ str = preg_replace ($ pat, "\ 1", $ str); echo $ str. "<br>"; // Note: when the regular expression is written as $ pat = "/(\ w +)-) {6} (\ d +)/I "; same as The preceding case. // 3 when the parameter is an array (in The example in the following manual) $ string = "The quick brown fox jumped over the lazy dog. "; $ patterns [0] ="/quic K/"; $ patterns [1] ="/brown/"; $ patterns [2] ="/fox/"; $ replacements [2] =" bear "; $ replacements [1] = "black"; $ replacements [0] = "slow"; print preg_replace ($ patterns, $ replacements, $ string ). "<br>"; // you may have discovered that it is processed in the order in which the key name appears in the array. This is not necessarily the same as the numerical order of the index. "In my understanding, it is in the order of" write ". This is not necessarily the order of // index (there is also ksort () in the book () in the later example, the time relationship will not be mentioned, and interested brothers can look for it by themselves) // if the subject is an array, each project in the subject will be searched and replaced, and returns an array. // If both pattern and replacement are arrays, preg_replace () extracts values from them to search and replace subject. If the value in replacement is less than that in pattern, an empty string is used as the remaining replacement value. If pattern is an array and replacement is a string, this string is used as the replacement value for each value in pattern. In turn, it makes no sense. // Preg_split // array preg_split (string pattern, string subject [, int limit [, int flags]) // returns an array, it contains the substrings separated by subject along the boundary matched with pattern. // If the limit parameter is specified, the limit substring is returned at most. If the limit parameter is-1, $ seek = array () is not restricted (); $ text = "I have a dream that one day I can make it. so just do it, nothing is impossible! "; // Split the string by blank space and punctuation marks (each punctuation may be followed by a space) $ words = preg_split ("/[.,;! \ S '] \ s */", $ text); foreach ($ words as $ val) {$ seek [strtolower ($ val)] ++ ;} echo "approximately ". count ($ words ). "words. "; Echo" contains a total of ". $ seek ['I]." Words "I ". ";?>

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.