Regular expression: ends with a forward slash (/) or a string.

Source: Internet
Author: User
Regular expression for help: the regular expression ends with a pattern or string & nbsp; I want a matching pattern followed by or ended with a string. The requirement is as follows: change the two domain names to three (add www .) original string: a.com & nbsp; or & nbsp; http: help regular expression: end with a/or string after a pattern
I want to end a matching pattern with a/or string.

The requirement is as follows: change the two domain names to three (add www .)

Original string: http://a.com or http://a.com/or http://www.a.com/
Replace with: http://www.a.com

Http://k.a.com cannot be replaced with http://www.k.a.com

I wrote:
Preg_replace ('/(\/) ([^. \/] + \. [^. \/] + $)/I ',' $ 1www. $2 ', 'http: // weibo.com ');
Preg_replace ('/(\/) ([^. \/] + \. [^. \/] + \/)/I ',' $ 1www. $2 ', 'http: // weibo.com /');
It needs to be replaced twice. can I put \/and $ together, similar to [a2]?
------ Solution --------------------
Preg_replace ('/(\/) ([^. \/] + \. [^. \/] + (? :\/
------ Solution --------------------
$)/I ',' $ 1www. $ 2', 'http: // weibo.com ');

------ Solution --------------------
Try
Echo preg_replace ('/(\/) ([^. \/] + \. [^. \/] +) ($
------ Solution --------------------
\/) (. + )? /Ie ',' "$1". "www.". "$2". ("$4 "? "/". "$4": "") ', 'http: // weibo.com/aa ');

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.