正则表达式,一些例子

来源:互联网 发布:游戏代练app源码 编辑:程序博客网 时间:2024/06/12 01:48
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.317.1.10.10.10127.0.0.1192.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);}}

0 0
原创粉丝点击