java获取字符串中的ip地址字符串为jdbcdmlocalhost5236PLS
可以使用正则表达式来提取字符串中的IP地址。以下是一个示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPAddressExtractor {
public static void main(String[] args) {
String str = "jdbc:dm://localhost:5236/PLS";
String ipAddress = extractIPAddress(str);
System.out.println(ipAddress);
}
public static String extractIPAddress(String str) {
String ipAddress = "";
// 定义IP地址的正则表达式
String regex = "\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
ipAddress = matcher.group();
}
return ipAddress;
}
}
输出结果为:localhos
原文地址: https://www.cveoy.top/t/topic/iq8w 著作权归作者所有。请勿转载和采集!