不多说直接上代码
public static void getHostFrom(String url) {
Pattern p = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*");
Matcher m = p.matcher(url);
if(m.find()) {
System.out.println(m.group("db"));
System.out.println(m.group("host"));
System.out.println(m.group("port"));
System.out.println(m.group("dbName"));
}
}
public static void main(String[] args) {
getHostFrom("jdbc:mysql://127.0.0.1:3306/databaseName");
getHostFrom("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=databaseName");
getHostFrom("jdbc:oracle:thin:@localhost:1521:dbName");
}