Get domain from strange yet valid url with java

Dennis Glot :

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.

Karol Dowbecki :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=73268&siteId=1