strangeQuirks
strangeQuirks

Reputation: 5920

Regex to properly match urls with a particular domain and also if there is a subdomain added

I have the following regex:

(^|^[^:]+:\/\/|[^\.]+\.)hello\.net

Which seems to work fors most cases such as these:

http://hello.net
https://hello.net
http://www.hello.net
https://www.hello.net
http://domain.hello.net
https://solutions.hello.net
hello.net
www.hello.net

However it still matches this which it should not:

hello.net.domain.com

You can see it here: https://regex101.com/r/fBH112/1

I am basically trying to check if a url is part of hello.net. so hello.net and any subdomains such as sub.hello.net should all match. it should also match hello.net/bye. So anything after hello.net is irrelevant.

Upvotes: 1

Views: 268

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

You may fix your pattern by adding (?:\/.*)?$ at the end:

(^|^[^:]+:\/\/|[^.]+\.)hello\.net(?:\/.*)?$

See the regex demo. The (?:\/.*)?$ matches an optional sequence of / and any 0 or more chars and then the end of string.

You might consider a "cleaner" pattern like

^(?:\w+:\/\/)?(?:[^\/.]+\.)?hello\.net(?:\/.*)?$

See the regex demo. Details:

  • ^ - start of string
  • (?:\w+:\/\/)? - an optional occurrence of 1+ word chars, and then :// char sqequence
  • (?:[^\/.]+\.)? - an optional occurrence of any 1 or more chars other than / and . and then .
  • hello\.net - hello.net
  • (?:\/.*)?$ - an optional occurrence of / and then any 0+ chars and then end of string

Upvotes: 1

Related Questions