I have already viewed and tried multiple other threads and doesn't work for me. I need the regex solution for it to work and no java code that does it without regex.
Some of the threads which I have already checked: Get domain name from given url, Extract host name/domain name from URL string, and Java regex to extract domain name? None work for me, either the regex doesn't work or the solution is a java code without regex.
What I am trying to do?
Case 1:
Input: https://api.twitter.com/blog/category/2?user=42&status=enabled
Output: api.twitter.com
Input: abc.xyz.com/blog/category/2?user=42&status=enabled
Output: abc.xyz.com
Case 2:
Input: https://abc.xyz.com/blog/category/2?user=42&status=enabled
Output: xyz.com
Input: abc.xyz.com/blog/category/2?user=42&status=enabled
Output: xyz.com
I need 2 regexes to solve each case mentioned above. If it can be done in one, even that works.
I tried the below regex from the first post:
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
This one works when there is https://
or any scheme but fails when there is no scheme in the URL.
So far I am solving the first case using a 2 step solution.
Step 1: Replace scheme
(.*://)(.*) -> $2
remove anything before and including string "://"
Step 2: Extract host name
([^/]*)(.*) -> $1
The first group extracts everything that is before the first "/". Basically extracting everything that isn't slash till I see the first one.