I have questions about regular expressions.

Source: Internet
Author: User
If you have any questions about regular expressions, please consult! Newbie, when posting an article publishing tool on the book, there is something wrong with the regular expression matching URL. $ Pattern & nbsp; (| )? [A-zA-Z0-9-] +.) + (com | cn | org | net) ([w % & amp;?.] *)?; $ String & nbsp; for beginners, you need to consult if you have any regular expressions!
Newbie, when posting an article publishing tool on the book, there is something wrong with the regular expression matching URL.

$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
(
Http://demo.demo.baidu.com
)
[1] => Array
(
[0] => http ://
)

[2] => Array
(
[0] => baidu.
)
[3] => Array
(
[0] => com
)
[4] => Array
(
[0] =>
)
)
[/Php]
The second subexpression actually matches 'www. 'and 'Baidu.', but only one matching T.T is returned.
The whole expression can match the whole URL, but I want to use the second subexpression to match the result twice. now there is only one. please advise where the problem is!
Thank you!
------ 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
(
[0] =>
)

)

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.