Andy
Andy

Reputation: 19261

Regex to match simple domain

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

Answers (6)

solve it
solve it

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

anubhava
anubhava

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 Demo

RegEx Details:

  • ^: Start
  • (?:https?:\/\/)?: Match http:// or https://
  • (?:[^.]+\.)?: Optionally Match text till immediately next dot and dot
  • example\.com: Match example.com
  • (\/.*)?: Optionally Match / followed by 0 or more of any characters
  • $: End

Upvotes: 14

K20GH
K20GH

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

Brad Parks
Brad Parks

Reputation: 72101

The following works in Java,

^(http:|https:|)[/][/]([^/]+[.])*example.com$

and matches your test cases, and doesn't match cases like

http://www.google.com/http://example.com

Upvotes: 2

Daniel
Daniel

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

itea
itea

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

Related Questions