Novice and regular related questions need to ask

Source: Internet
Author: User
Novice and have a regular problem need to ask!
Novice, when playing the book on the article Publishing tool, with regular matching URL out of a point.

$pattern = '/(http\:\/\/|https\:\/\/)? ([a-za-z0-9-]+\.) + (com|cn|org|net) (\/[\w\%\&\=\ ' \ "\?\/\. *)?/';
$string = ' http://demo.demo.baidu.com ';
Preg_match_all ($pattern, $string, $result);

The result of traversing $result is
[code=php]
Array
(
[0] = = Array
(
[0] = http://demo.demo.baidu.com
)
[1] = = Array
(
[0] =/http
)

[2] = = Array
(
[0] = Baidu.
)
[3] = = Array
(
[0] = = com
)
[4] = = Array
(
[0] = =
)
)
[/php]
The second sub-expression actually matches the ' www. ' and ' Baidu. ', there are two matches, but there is only one match in the returned results t.t
The entire expression is able to match the entire URL, but I want to use the second sub-expression of the two-match results, now only one, I hope you teach the question where to appear!
Thank!
------Solution--------------------
(http\:\/\/
------Solution--------------------
https\:\/\/)? ([a-za-z0-9-]+\.) +) + (com
------Solution--------------------
cn
------Solution--------------------
Org
------Solution--------------------
NET) (\/[\w\%\&\=\ ' \ "\?\/\. *)?

------Solution--------------------
$pattern = '/(http\:\/\/
------Solution--------------------
https\:\/\/)? ((?: [A-za-z0-9-]+\.) +) (COM
------Solution--------------------
cn
------Solution--------------------
Org
------Solution--------------------
NET) (\/[\w%&=\ ' "? \.] *)?/';
$string = ' http://demo.demo.baidu.com ';
Preg_match_all ($pattern, $string, $result);
Array
(
[0] = = Array
(
[0] = http://demo.demo.baidu.com
)

[1] = = Array
(
[0] =/http
)

[2] = = Array
(
[0] = = Demo.demo.baidu.
)

[3] = = Array
(
[0] = = com
)

[4] = = Array
(

)

)

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