Jayashree
Jayashree

Reputation: 11

java regex to ignore the domain name in a URL and consider everything beyond

Here is my case:

I need everything beyond the domain name in the URL, i.e, everything beyond the third occurence of / Here is the sample URL: https://test-qa-documents.ss-us-abc-2.mentor.com/thumbnail/58043233791488_testfit_image?X-Amz-Security-Token=xyz

I need the output as: thumbnail/58043233791488_testfit_image?X-Amz-Security-Token=xyz

Any help on this?

Upvotes: 0

Views: 244

Answers (2)

Kerwin
Kerwin

Reputation: 1212

url.replaceFirst("https?://[^/]+/","")

Just replace http://.../ part to blank.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521914

The Java URL class has support many types of parsing URLs. In this case, you can use URL#getFile() on your URL to obtain the string portion you want.

// import java.net.*
String url = "https://test-qa-documents.ss-us-abc-2.mentor.com/thumbnail/58043233791488_testfit_image?X-Amz-Security-Token=xyz";
URL theURL = new URL(url);
System.out.println(theURL.getFile());

Output:

/thumbnail/58043233791488_testfit_image?X-Amz-Security-Token=xyz

Upvotes: 1

Related Questions