4

Im trying to parse the bunch of url.I tried using the URI class in java but it is not perfect for the different set of strings. And also i have to do the parsing on IPV6.The input strings is given below.

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/database?user=userName&password=pass
jdbc:postgresql://host:port/database?charSet=LATIN1&compatible=7.2

jdbc:postgresql://[::1]:5740/accounting (IPV6)

I want the hostname,port,and database name.

Guide me !

JAVA Beginner
  • 389
  • 3
  • 15

1 Answers1

3
    String cleanURI = s.substring(5);
    URI uri = URI.create(cleanURI);
    System.out.println(uri.getScheme());
    System.out.println(uri.getHost());
    System.out.println(uri.getPort());
    System.out.println(uri.getPath());
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35