java正则表达式解析jdbcUrl
一个练习正则表达式的网站:https://regexr.com/
用java解析jdbcUrl
public static void main(String[] args) throws SQLException { String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/database?a=1&b=2"; String pattern="jdbc:(?<type>[a-z]+)://(?<host>[a-zA-Z0-9-//.]+):(?<port>[0-9]+)/(?<database>[a-zA-Z0-9_]+)?"; Pattern namePattern = Pattern.compile(pattern); Matcher dateMatcher = namePattern.matcher(jdbcUrl); while (dateMatcher.find()) { String dbType = dateMatcher.group("type"); String host = dateMatcher.group("host"); String port = dateMatcher.group("port"); String database = dateMatcher.group("database"); System.out.println(dbType); System.out.println(host); System.out.println(port); System.out.println(database); } }
输出:
mysql 127.0.0.1 3306 database