Php Regular Expression learning notes

Source: Internet
Author: User
Tags php regular expression
This article mainly introduces how to learn php regular expressions, learn php Regular Expressions in detail, and learn more about php regular expressions. If you are interested, refer

This article mainly introduces how to learn php regular expressions, learn php Regular Expressions in detail, and learn more about php regular expressions. If you are interested, refer

Php Regular Expression learning notes:

1. Create a regular expression

$ Regex = '/\ d/I ';
It is a bit like the first method in JavaScript, but here it is a string.

2. special characters in Regular Expressions

Special characters include:. \ + *? [^] $ () {}=! <> | :-

3. Functions in Regular Expressions

There are eight methods,Preg_match and preg_match_all, preg_replace and preg_replace_callback, preg_grep, preg_split, preg_last_error, and preg_quote.

Preg_match:

Execute a regular expression match

Returns the matching times of pattern. Its value will be 0 (not matched) or 1 time, because preg_match () will stop searching after the first match.

$ Subject = "dd133aa2"; $ pattern = '/\ d +/'; preg_match ($ pattern, $ subject, $ matches, PREG_OFFSET_CAPTURE); print_r ($ matches );

The above sample code adds the parameter "PREG_OFFSET_CAPTURE". In this way, there will be an offset in $ matches. For example"

Preg_match_all:

Execute a global regular expression to match

Returns the number of complete matches (which may be 0), or returns FALSE if an error occurs.

In the following code, $ subject and $ pattern are the same as above. The only difference is that preg_match is replaced with preg_match_all.

Preg_match_all ($ pattern, $ subject, $ matches, PREG_OFFSET_CAPTURE); print_r ($ matches );

The number of times returned is 2, matching twice. Then, let's look at the input in the array $ matches. There are two. It is one more than above.

Preg_replace:

Search and replace a regular expression

If the subject is an array, preg_replace () returns an array, and otherwise returns a string.

If the match is found, the replaced subject is returned. Otherwise, the unchanged subject is returned. If an error occurs, NULL is returned.

In the code, $ subject is an array containing two strings. Next, replace multiple numbers with uppercase "Z ".

$ Subject = array ("dd133aa2", "kk1ff3"); $ pattern = '/\ d +/'; $ result = preg_replace ($ pattern, 'z ', $ subject); print_r ($ result );

Preg_replace_callback:

Execute a regular expression search and use a "Callback" to replace

In addition to specifying a callback to replacement for string replacement, this function is equivalent to preg_replace (), including the returned results.

The following code is replaced with the uppercase "Z". The content in $ matches in the callback function is the comments in the Code. The first is 133, and the second is 2.

$ Subject = "dd133aa2"; $ pattern = '/\ d +/'; $ result = preg_replace_callback ($ pattern, function ($ matches) {// $ matches [0] => 133 // $ matches [0] => 2 return 'Z' ;}, $ subject); print_r ($ result );

Preg_grep:

Returns array entries in matching mode.

Use the key in the inputIndexed Array.

In the following sample code, I added a "ddsdfd" to the $ subject array, which does not contain numbers. During the matching, filter out the unnumbered ones.

In contrast, $ result2 prints out the filter, but the key is still 2, not 0.

$ Subject = array ("dd133aa2", "kk1ff3", "ddsdfd"); $ pattern = '/\ d +/'; $ result = preg_grep ($ pattern, $ subject); $ result2 = preg_grep ($ pattern, $ subject, PREG_GREP_INVERT); print_r ($ result); print_r ($ result2 );


Preg_split:

Use a regular expression to separate strings

Returns an array consisting of substrings separated by the pattern boundary.

In the following code, I add brackets to the expression in $ pattern to capture the expression in $ result2.

$ Subject = "dd133aa2cc"; $ pattern = '/(\ d +)/'; $ result = preg_split ($ pattern, $ subject); $ result2 = preg_split ($ pattern, $ subject, null, PREG_SPLIT_DELIM_CAPTURE); print_r ($ result); print_r ($ result2 );


Preg_last_error:

Returns the error code generated by the last PCRE regular execution.

Preg_match ('/(? : \ D + | <\ d +>) * [!?] /', 'Foobar foobar foobar'); $ result = preg_last_error (); // The PREG_BACKTRACK_LIMIT_ERROR callback limit exceeds print_r ($ result );

Preg_quote:

Escape Regular Expression characters

ReturnEscape string.

In the following code, $ subject contains two escape characters: "." and "?".

After $ result is printed, it is "dd \. \? A2cc. In $ result2, a parameter "a" is added. In this case, "a" is also escaped, "dd \. \ \? \ A2cc"

$ Subject = "dd.? A2cc "; $ result = preg_quote ($ subject); $ result2 = preg_quote ($ subject, 'A'); print_r ($ result); print_r ($ result2 );

4. Pattern Modifier

The above is a full introduction to php regular expressions, and I hope it will be helpful for your learning.

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.