正则表达式,一些例子
发布时间:2020-12-14 01:11:26 所属栏目:百科 来源:网络整理
导读:package cn.zhengze;import java.util.ArrayList;import java.util.Arrays;public class zhengze {/** * @param args */public static void main(String[] args) { //test1(); //test2(); test3();}public static void test1() { String temp = "我我我.....
package cn.zhengze; import java.util.ArrayList; import java.util.Arrays; public class zhengze { /** * @param args */ public static void main(String[] args) { //test1(); // test2(); test3(); } public static void test1() { String temp = "我我我.......我要学...学学学...编编编程程.."; temp=temp.replaceAll(".+",""); temp=temp.replaceAll("(.)1+","$1"); System.out.println(temp); } private static void test2() { /* * 对ip地址排序 * */ String temp = "192.168.1.200 17.1.10.10.10 3.3.50.3 127.0.0.1"; temp=temp.replaceAll("(d+)","00$1" ); temp =temp.replaceAll("0*(d{3})","$1"); String[] ips =temp.split(" +"); Arrays.sort(ips); for(String ip:ips) { System.out.println(ip.replaceAll("0*(d+)","$1")); } /* 结果: * 3.3.50.3 17.1.10.10.10 127.0.0.1 192.168.1.200 */ } private static void test3() { /* * 校检邮箱地址 */ String mail = "abc12@sina.com"; String regex="w+@[a-zA-Z0-9]+(.[a-zA-Z]{2,3}){1,3}"; boolean b =mail.matches(regex); System.out.println(mail+":"+b); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |