How can I make the following regex also match

Viewed 82

I have the following regex:

/^(https?:\/\/)(.+\.)?(.+)(\..*)$/gm

The list is:

https://localhost
https://www.example.com
https://www.subdomain.example.com
https://example.com
http://example.com
http://example..com
http://....example.com

It matches:

https://www.example.com
https://www.subdomain.example.com
https://example.com
http://example.com
http://example..com
http://....example.com

The problem is that, I want to match https://localhost, and I do not want to match:

http://example..com
http://....example.com

Why am I not using just /^https?:\/\/.+$/gm? Because I need to capture them in groups. So by that, I mean:

For https://www.subdomain.example.com

Group 1: https://
Group 2: www.subdomain.
Group 3: example
Group 4: .com

And for https://localhost, it would be:

Group 1: https://
Group 3: localhost

For https://www.example.com:

Group 1: https://
Group 2: www.
Group 3: example
Group 4: .com

So, in:

https://localhost
https://www.example.com
https://www.subdomain.example.com
https://example.com
http://example.com
http://example..com
http://....example.com

I only want to match:

https://localhost
https://www.example.com
https://www.subdomain.example.com
https://example.com
http://example.com

Btw, it should not match invalid urls:

http:
https://

Only full urls.

Btw, any subdomains.

Here is a link: https://regex101.com/r/z7V33F/1

How can I achieve this?

3 Answers

You may use this regex:

^(https?://)((?:[\w-]+\.){0,2}?)([^.]+)(\.[^.]+)?$

RegEx Demo

RegEx Details:

  • ^: Start
  • (https?://): Match https:// or http:// and capture in group #1
  • ((?:[\w-]+\.){0,2}?): Match first part of domain and capture in optional group #2
  • ([^.]+): Match (last-1)th part of domain name and capture in group #3
  • (\.[^.]+)?: Match last part of domain and capture in *optional& group #4
  • $: End

You can use

^(https?:\/\/)((?:[^.\/]+\.)*?)([^.\/\n]+)(\.[^.\/]+)?$
^(https?://)((?:[^./]+\.)*?)([^./]+)(\.[^./]+)?$

See the regex demo. The second pattern variant is for those engines that do not require / regex delimiters. Details:

  • ^ - start of string
  • (https?:\/\/)? - an optional Group 1: https:// or http://
  • ((?:[^.\/]+\.)*?) - Group 2: zero or more occurrences (but as few as possible) of one or more chars other than . and / followed with a .
  • ([^.\/]+) - Group 3: one or more chars other than . and /
  • (\.[^.\/]+)? - an optional Group 4: a . char and then one or more chars other than . and /
  • $ - end of string.

What about:

^(https?:\/\/)(www\.(?:[^.\n]+\.)??)?([^\.\n]+)(\.[^\.\n]+)?$

See the online demo

  • ^ - Start string anchor.
  • (https?:\/\/) - 1st Capture group holding "http", an optional "s" and the two literal forward slashes.
  • (www\.(?:[^.\n]+\.)??)? - A 2nd optional capture group holding literally "www" followed by a literal dot and an optional lazy group of 1+ characters that are not a dot and newline character untill a literal dot.
  • ([^\.\n]+) - A 3rd capture group holding 1+ character not being literal dot or newline character.
  • (\.[^\.\n]+)? - Optional 4th capture group holding a literal dot and 1+ characters not being a dot or newline character.
  • $ - End string anchor.
Related