关于正则表达式的一道面试题

前端之家收集整理的这篇文章主要介绍了关于正则表达式的一道面试题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@这里写代码片 @H_404_1@import java.util.Arrays; public class ip_test { /** * "172.25.27.1 3.25.118.32 105.38.225.12" * 对上面字符串把ip地址切割出来进行排序 * */ public static void main(String[] args) { String ip = "172.25.27.1 3.25.118.32 105.38.225.12"; System.out.println("原字符串:"+ip); ip=ip.replaceAll("(\\d+)","00$1"); System.out.println("先补0:"+ip); ip=ip.replaceAll("0*(\\d{3})","$1"); System.out.println("留三位:"+ip); String[] new_ip = ip.split(" +"); // System.out.println(new_ip);//这样打印的是字符串数组的首地址 Arrays.sort(new_ip); System.out.println("去0再排序后:"); for (String string : new_ip) { System.out.println(string.replaceAll("0*([0-9]{1,2})","$1")); } } }

猜你在找的正则表达式相关文章