java正则匹配替换字符串中ip格式的字符串

来源:互联网 发布:游戏编程需要怎么学 编辑:程序博客网 时间:2024/05/19 02:40
String s = "lsx[ip:192.168.19.176]的[Physical Memory]...[ip:192.168.19.158]";
String reg = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; //匹配ip的正则
System.out.println(s.replaceAll(reg, "123456"));//替换全部
System.out.println(s.replaceFirst(reg, "123456"));//替换第一个
如下图: