How can I match my regex for URL (example.net/directory) without HTTP, HTPPS and WWW? [duplicate]

Solution for How can I match my regex for URL (example.net/directory) without HTTP, HTPPS and WWW? [duplicate]
is Given Below:

I have a string format with URLs. For example: “first string url is example.com/directory, second URL is http:///example.com/directory and 3rd is www.example.com/directory”

I want to match my regex exactly for “example.com/directory” without http and www

I am trying the following regex but this is also working for http, https and www.

(S+)(?:com|net|[/])[/](S+|$)

Don’t use a regex if you can, see if you can parse the url with a dedicated library

This will also help with other TLDs, such as .net, .org, .club.

>>> import urllib.parse
>>> urls = ("https://www.example.com/directory", "www.example.com/directory", "example.com/directory")
>>> for url in urls:
...     print(urllib.parse.urlparse("http://" + url.split("//")[-1]))
...
ParseResult(scheme="http", netloc="www.example.com", path="/directory", params="", query='', fragment="")
ParseResult(scheme="http", netloc="www.example.com", path="/directory", params="", query='', fragment="")
ParseResult(scheme="http", netloc="example.com", path="/directory", params="", query='', fragment="")

To get just the top and second-level domain, you could just split() the netloc

>>> urllib.parse.urlparse("http://whatever.example.com").netloc.split(".")[-2:]
['example', 'com']

To only allow for http:// and www. prefix (only one, if any). Then you can use optional alternatives:

^(?:http://|www.)?(w+.(?:com|net)/directory)$

Try it out here: https://regex101.com/r/aPtYhc/1

We use a capturing group to only capture the “example.com/directory” of the URL. This means that the RegEx will not capture everything it matches.

This regEx might help.

1 www.example.com/directory match:example.com/directory

2 http://example.com/directory match:example.com/directory

3 example.com/directory match:example.com/directory

4 example.net/directory match:example.net/directory

(?<=www.)[w./]+|(?<=http://)[w./]+|w+.com/w+|w+.net/w+

You could check regEx online here.

https://regex101.com/r/pEmcJP/1