io.github.xtazxz.base.common.utils.reg.RegUtils Maven / Gradle / Ivy
The newest version!
package io.github.xtazxz.base.common.utils.reg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则工具类.
*/
public class RegUtils {
private RegUtils() {
}
private static final String NAME_JDBC_HOSTNAME = "hostname";
private static final String NAME_JDBC_PORT = "port";
private static final String REG_JDBC =
"://(?<" + NAME_JDBC_HOSTNAME + ">.*):(?<" + NAME_JDBC_PORT + ">\\d+)/";
public static Jdbc parseJdbcUrl(String jdbcUrl) {
String hostname;
int port;
Pattern jdbcPattern = Pattern.compile(REG_JDBC);
Matcher matcher = jdbcPattern.matcher(jdbcUrl);
if (matcher.find()) {
hostname = matcher.group(NAME_JDBC_HOSTNAME);
port = Integer.parseInt(matcher.group(NAME_JDBC_PORT));
if (hostname == null || hostname.isEmpty()) {
throw new IllegalArgumentException(NAME_JDBC_HOSTNAME);
}
if (port <= 0) {
throw new IllegalArgumentException(NAME_JDBC_PORT);
}
return new Jdbc(hostname, port);
} else {
throw new IllegalArgumentException("Invalid JDBC URL");
}
}
}