Reputation: 19261
I am trying to match a simple domain: example.com
But all combinations of it.
How would I do this to cover:
https://example.com http://www.example.com etc.
Upvotes: 20
Views: 69830
Reputation: 301
The below exp matches for http/htpps/ftp in the first part, though it can also match random 5 letter word like ahfzc but that rarely would be case and they would be ignored by later part of the exp
The second part matches for ww/www and the last part matches for any alphanumeric seperated by '.'. And the last part matches for any 3 character like .com,.in,.org etc.
try this
r'[a-z0-9]{0,5}[\:\/]+[w]{0,3}[\.]+[a-z0-9\-]+[\.]+[a-z0-9]{0,3}'
Upvotes: 1
Reputation: 785531
You can probably use to just match the domain name part of a URL:
/^(?:https?:\/\/)?(?:[^.]+\.)?example\.com(\/.*)?$
It will match any of following strings:
https://example.com
http://www.example.com
http://example.com
https://example.com
www.example.com
example.com
RegEx Details:
^
: Start(?:https?:\/\/)?
: Match http://
or https://
(?:[^.]+\.)?
: Optionally Match text till immediately next dot and dotexample\.com
: Match example.com
(\/.*)?
: Optionally Match /
followed by 0 or more of any characters$
: EndUpvotes: 14
Reputation: 6281
This will correctly match the URL for any variation of the below, plus anything after .com
https://example.com
https://www.example.com
http://www.example.com
http://example.com
https://example.com
www.example.com
example.com
Result will be either true or false
const result = /^(http(s)?(:\/\/))?(www\.)?example\.com(\/.*)?$/.test(value);
Upvotes: 2
Reputation: 72101
The following works in Java,
^(http:|https:|)[/][/]([^/]+[.])*example.com$
and matches your test cases, and doesn't match cases like
Upvotes: 2
Reputation: 1799
A more generic example I used:
/http(?:s)?:\/\/(?:[\w-]+\.)*([\w-]{1,63})(?:\.(?:\w{3}|\w{2}))(?:$|\/)/i
Note that this solution doesn't pick up the correct label for 5 character TLDs. Example:
http://mylabel.co.uk
Would be picked up as 'co' instead of 'mylabel', but
http://mylabel.co
would be matched correctly as 'mylabel'. The regex was good enough for me even with this limitation.
Note that the 63 character limit for the label is an RFC specification. Hope this helps anyone looking for the same answer in the future.
Upvotes: 6
Reputation: 444
^https?://([\w\d]+\.)?example\.com$
using code:
var result = /^https?:\/\/([a-zA-Z\d-]+\.){0,}example\.com$/.test('https://example.com');
// result is either true of false
I improved it to match like "http://a.b.example.com"
Upvotes: 14