1

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.

strangeQuirks
  • 4,761
  • 9
  • 40
  • 67

1 Answers1

1

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563