Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain from strange yet valid url with java

Tags:

java

url

I need to get host from this url

android-app://com.google.android.googlequicksearchbox?Pub_id={siteID} 

java.net.URL and java.net.URI can't handle it.

like image 735
Dennis Gloss Avatar asked Jan 02 '23 10:01

Dennis Gloss


1 Answers

The problem is in { and } characters which are not valid for URI. Looks like a placeholder that wasn't resolved correctly when creating a URI.

You can use String.replaceAll() to get rid of these two characters:

String value = "android-app://com.google.android.googlequicksearchbox?Pub_id={siteID}";
URI uri = URI.create(value.replaceAll("[{}]", ""));
System.out.println(uri.getHost()); // com.google.android.googlequicksearchbox
like image 163
Karol Dowbecki Avatar answered Jan 13 '23 14:01

Karol Dowbecki