php - Regex to match URL except for some websites using Look Ahead

Solution:
If you just want to require a URL to havehttp
,www
andcom
in it, but notmysite
in the host part, then you may use
preg_match('~^https?://(?:[^./]+\.)*(?!mysite)[^./]+\.com(?:/.*)?$~', $url)
See the regex demo
Details:
^
- start of stringhttps?://
-http://
orhttps://
(?:[^./]+\.)*
- zero or more sequences of:[^./]+
- 1+ chars other than.
and/
\.
- a literal dot
(?!mysite)[^./]+
- 1+ chars other than.
and/
that - up to.com
- does not equalmysite
\.com
- a.com
string(?:/.*)?
- 1 or 0 sequences of/
- slash.*
- any 0+ chars other than line break chars
$
- end of string.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: property [id] does not exist on this collection instance.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.