Greedy pattern regular matching

Source: Internet
Author: User
Body: {code ...} expected: {code ...} method 1: {code ...} result: {code ...} method 2: {code ...} result: {code ...} what should I do if I want to get the expected value? Body:

$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';

Expected:

[0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com

Method 1:

$pat = '/http:\/\/(.*)\.uz\.taobao\.com/is';

Result:

[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com 

Method 2:

$pat = '/http:\/\/(.*)\.uz\.taobao\.com/iUs';

Result:

[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com [1]=>http://yy.uz.taobao.com 

What should I do if I want to get the expected value?

Reply content:

Body:

$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';

Expected:

[0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com

Method 1:

$pat = '/http:\/\/(.*)\.uz\.taobao\.com/is';

Result:

[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com 

Method 2:

$pat = '/http:\/\/(.*)\.uz\.taobao\.com/iUs';

Result:

[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com [1]=>http://yy.uz.taobao.com 

What should I do if I want to get the expected value?

$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';preg_match_all('/(https?:\/\/[^.]+\.uz\.taobao\.com)/i', $content, $match);print_r($match[1]);

So easy.


    array(2) {    [0] =>    string(23) "http://xx.uz.taobao.com"    [1] =>    string(23) "http://yy.uz.taobao.com"  }}

$content = ' sdfasdfasfas sdfsfasf http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';$urls = explode('http://', $content);array_shift($urls);$res = [];foreach($urls as &$url) {    $item = explode('taobao.com', $url);    $url = "http://{$item[0]}taobao.com";    if(preg_match('/uz\.taobao\.com/', $url))        $res[] = $url;}print_r($res);//Array ( [0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com )
$content = ' sdfasdfasfas sdfsfasf http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';preg_match_all('/(http[^taobao]*?uz\.taobao\.com)/i', $content, $match);print_r($match[1]);//Array ( [0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com )

Another small suggestion is: Split the URL into two steps first, and then match each URL. In this way, the semantic intuition can be enhanced.

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.