PHP preg Regular Expressions

Source: Internet
Author: User
Tags preg expression engine


Note:

Backslashes have special meanings in both single-quote strings and double-quote strings, so to match a backslash, the pattern must be written as "\\\\". "/\\/", first it as a string, the backslash will be escaped, then the result of escaping is/\/, this is the regular expression engine to get the pattern, and the regular expression engine also think \ is an escape token, it will be the delimiter/escaped, resulting in an error, so it takes 4 backslashes to Can match a backslash.

In fact

$attribute = "sdfsdf.555";p reg_match ('/([\w]+) (\.) ([0-5]|.) /', $attribute, $matches); Array (    [0] = sdfsdf.555    //Match ([\w]+) (\.) ([0-5]|.)  If the entire pattern match fails, the match will not continue. For example $attribute = "sssss555"    [1] = = SDFSDF    //Match (\w+)    [2]    = =. Match (\.)    [3] = = 555    //Match ([0-5]*))

Preg_replace_callback

Each value that is matched to a character channeling expression is processed and returned with a callback function:

The function will continue searching after the first match.

The first match gets [{summary}]

Then it continues to search for matches to get [{items}]

Last match to get [{form}]

The next example: Regular Expressions: The results of the match were: [{Summary}],[{items}] [{form}]

Use three values for a callback function ...

Compare the differences between the following two matching values:

$layout = "{summary}\n{items}\n{form}"; $content = Preg_replace_callback ("/{\\w+}/", function ($matches) {Print_r ($ matches);  return $matches [0];}, $layout);p Rint_r ($content); $layout = "{summary}-{items}-{form}"; $content = Preg_replace_ Callback ("/({\\w+}) (-)/", function ($matches) {print_r ($matches);//["{summary}-", "{Summary}", "-"]return $matches [0 ], $layout);p Rint_r ($content);

PHP preg Regular Expressions

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.