Usage of non-greedy pattern matching in php regular expressions

Source: Internet
Author: User
Here, we will briefly introduce the usage of non-greedy pattern matching in php regular expressions through examples. if you need it, let's take a look at it. we usually write it like this:

The code is as follows:


$ Str = "http://www.baidu/.com? Url = www.sina.com /";
Preg_match ("/http :(. *) com/", $ str, $ matches );
Print_r ($ matches );

Result:

The code is as follows:


Array ([0] => http://www.baidu/.com? Url = www.sina.com [1] => // www. baidu/. com? Url = www. sina .)

Non-greedy pattern matching:

The code is as follows:


$ Str = "http://www.baidu/.com? Url = www.sina.com /";
Preg_match ("/http :(.*?) Com/", $ str, $ matches );
Print_r ($ matches );

Result:

The code is as follows:


Array ([0] => http://www.baidu/.com [1] => // www. baidu /.)

Simply put, as long as a character is followed by a specified number of special characters, matching is a non-greedy mode. Have you understood this?

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.