使用正则拆分url
/**
* 从url获取ip、port和地址信息
* 例如 入参:rtmp://10.0.0.1:6666/live/a174658f3ba9483e9c597fb7c4d2bd2d_sub
* 出参:[10.0.0.1, 6666, /live/a174658f3ba9483e9c597fb7c4d2bd2d_sub]
* @param url
* @return
*/
public static Map<String,String> getInfoFromUrl(String url){
Map<String,String> infos = new HashMap<>(3);
// Pattern p = Pattern.compile("(\\w+):\\/\\/([^/:]+)(:\\d*)?([^# ]*)")
Pattern p = Pattern.compile("(\\w+)://([^/:]+):(\\d*)?([^# ]*)");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
infos.put("ip",matcher.group(2));
infos.put("port",matcher.group(3));
infos.put("urlName",matcher.group(4));
}
return infos;
}
/**
* 从url获取ip信息
* 例如 入参:rtsp://10.0.0.1:655/Eurl/qEPs1Km
* 出参:10.0.0.1
* @param url
* @return
*/
public String getIpFromUrl(String url){
String ip = "";
Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
ip = matcher.group();
}
return ip;
}
替换字符串占位符
https://www.cnblogs.com/dongma/p/10274836.html
/**
*
* @param template 模板
* @param prefix 前缀
* @param suffix 后缀
* @param prop 键值对
* @return 解析好的字符串模板
*/
public static String parseValue(String template,String prefix,String suffix,Map<String,String> prop){
StringBuilder buf = new StringBuilder(template);
int startIndex = template.indexOf(prefix);
while (startIndex != -1){//找到了前缀
int suffixIndex = buf.indexOf(suffix, startIndex);//找到后缀位置
String key=buf.substring(startIndex+prefix.length(),suffixIndex);
// 进行字符串替换
if (prop.containsKey(key.trim())) {
buf.replace(startIndex, suffixIndex + suffix.length(), prop.get(key.trim()).toString());
suffixIndex = suffixIndex + prop.get(key.trim()).toString().length() - key.length() - prefix.length() - suffix.length();// 替换后修改后缀位置
}
startIndex = buf.indexOf(prefix, suffixIndex);//找到下一个占位的起始位置
}
System.out.println(buf);
return buf.toString();
}