/*
* 题目是
* 把ip地址按照从大到小的顺序排序.
* 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
*
*
* 解题思路;
* 1,按照每一段需要的最多的0进行补齐,那么每一段就会保证至少有3位.
* 2,将每一段只保留3位.这样,所有的ip 地址都是每一段3位.
*
* */
public class regexTest2 {
public static void main(String[] args){
//test_2();
jiaoYan();
}
public static void test_2(){
String ip=" 192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
ip=ip.replaceAll("(\\d+)","00$1");//这个地方这样做的目的是,将ip地址的
System.out.println(ip); //前面都加上两个0,因为要用带前面匹配的结果所以要用到组
ip=ip.replaceAll("0*(\\d{3})","$1");//这个地方这样做 是为了让其只保留3位.
System.out.println(ip);
String[] arr=ip.split(" ");
TreeSet<String> ts= new TreeSet<String>();
for(String s: arr){//因为要对,其进行自然排序,所以这里面建立一个,TreeSet集合
ts.add(s);
}
for(String s: ts){
System.out.println(s.replaceAll("0*(\\d+)", "$1"));
} //这个地方这样做是为了,把多余的 0 都给去掉了.
}
/*
* 现在有这样一个需求;
* 对邮箱地址值进行校验.
* 569613@qq.com
*
* */
public static void jiaoYan(){
String str="569613@qq.com";
String st="[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//这是比较精确匹配
st="\\w+@\\w+(\\.\\w+)+";//相对于来说这是不太精确的
System.out.println(str.matches(st));
}
}
黑马程序员 正则表达式的练习
最新推荐文章于 2020-08-19 11:52:49 发布