PHP Regular Expression Learning notes, regular expression learning notes _php Tutorial

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expression Learning notes, regular expression learning notes


PHP Regular Expression Learning notes sharing:

1. Create a regular expression

$regex = '/\d/i ';
It's a bit like the first way in JavaScript, but here is a string.

2. Special characters in regular expressions

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

3. Functions in regular expressions

There are 8 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:

Performs a regular expression match

returns the number of occurrences of pattern . Its value will be 0 times (mismatched) or 1 times, because Preg_match () will stop searching after the first match .

$subject = "Dd133aa2"; $pattern = '/\d+/';p reg_match ($pattern, $subject, $matches, preg_offset_capture);p Rint_r ($ matches);

The example code above adds the parameter "preg_offset_capture" so that there is an additional offset in the $matches. For example, the following "2"

Preg_match_all:

Performs a "global" regular expression match

Returns the complete number of matches (possibly 0), or False if an error occurs.

In the code below, $subject and $pattern are the same as above, and the only difference is that Preg_match is replaced with Preg_match_all.

Preg_match_all ($pattern, $subject, $matches, preg_offset_capture);p Rint_r ($matches);

The number of returns is 2, matched to two times, and then the input in the array $matches, with two. One more than the above.

preg_replace:

Perform a search and replace of a regular expression

If subject is an array, preg_replace () returns an array, and in other cases returns a string.

If the match is found, the replaced subject is returned, and other cases return unchanged subject. If an error occurs, NULL is returned.

In the code, $subject is an array with two sets of strings, followed by replacing multiple numbers with uppercase "Z".

$subject = Array ("Dd133aa2", "KK1FF3"), $pattern = '/\d+/'; $result = preg_replace ($pattern, ' Z ', $subject);p Rint_r ($ result);

Preg_replace_callback:

Performs a regular expression search and replaces it with a "callback"

The behavior of this function, in addition to specifying a callback substitution replacement for substitution string calculations, is equivalent to Preg_replace (), including the returned result.

The following code is also replaced with uppercase "Z", the contents of each $matches in the callback function is the part of the comment in the code, the first time is 133, the second time is 2.

$subject = "Dd133aa2"; $pattern = '/\d+/'; $result = Preg_replace_callback ($pattern, function ($matches) {    //$matches [0] = = 133    //$matches [0] = 2    return ' Z ';   

Preg_grep:

Returns an array entry for the matching pattern

Returns an array that is indexed using key in input.

In the following example code, in the $subject array I added a "DDSDFD", which does not contain numbers, in the match, the number of the filter out.

and $result2 print out of the exact opposite, is to print out the filter, but the key is still 2, not 0.

$subject = Array ("Dd133aa2", "KK1FF3", "DDSDFD"); $pattern = '/\d+/'; $result = Preg_grep ($pattern, $subject); $result 2 = pr Eg_grep ($pattern, $subject, Preg_grep_invert);p rint_r ($result);p Rint_r ($result 2);


Preg_split:

Separating strings with a regular expression

Returns an array of substrings that are made using the pattern boundary to separate subject.

In the following code, I enclose the expression in $pattern with parentheses in order to capture it in $RESULT2.

$subject = "dd133aa2cc"; $pattern = '/(\d+)/'; $result = Preg_split ($pattern, $subject); $result 2 = Preg_split ($pattern, $ Subject, NULL, preg_split_delim_capture);p rint_r ($result);p Rint_r ($result 2);


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 ();//preg_backtrack_limit_error call backtracking limit exceeds print_r ($result);

Preg_quote:

Escaping regular expression characters

Returns the escaped string .

In the following code, there are two characters in the $subject that need to be escaped, "." and "?".

After the $result is printed out is "dd\.a\?a2cc", and in $result2, add a parameter "a", so that "a" will also be escaped, "dd\.\a\?\a2cc"

$subject = "dd.a?a2cc"; $result = Preg_quote ($subject); $result 2 = Preg_quote ($subject, ' a ');p Rint_r ($result);p Rint_r ($ RESULT2);

4. Mode modifier

The above is about the PHP regular expression of the full content of the introduction, I hope that everyone's learning has helped.

http://www.bkjia.com/PHPjc/1071224.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071224.html techarticle PHP Regular Expression Learning notes, regular expression learning notes PHP Regular Expression learning notes sharing: 1. Creating regular Expressions $regex = '/\d/i '; with JavaScript the first way a bit ...

  • 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.