正则表达式的认识

先看链接 分类为java高级 建议都看看:http://www.cnblogs.com/yangming1996/p/7168353.html 

以下代码是自己联系的。直接粘贴复制可用。 

package com.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.nio.cs.ext.MacHebrew;

/**
 * 正则表达式练习
 * @author 乔成强
 * @version 创建时间:2018年5月16日  上午11:15:43
 * 正则表达式学习链接:http://deerchao.net/tutorials/regex/regex.htm
 */
public class RegexStudy {
    
    public static void main(String [] args){
        
        String regex = "^[0-9]+abc$";  //正则表达式 
        //^以什么开头
        //[0-9]代表0到9之间任意的单个数字  [0-9]+代表匹配多个数字
        //abc$ 代表带有abc字母并且以abc字母结尾
        Pattern p = Pattern.compile(regex);  //将给定的正则表达式编译并赋予给Pattern类
        String [] strs = {"abc12abc","1a2b3c","abc123","456abc","abcabc","4abc","abc123"};  //需要正则表达式匹配验证的字符串数组
        for (String str : strs) {
            boolean b = str.matches(regex);
            //System.out.println(str.matches(regex));
            if(b){
                System.out.print(str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         *   ’*’,’+’和’?’这三个符号,表示一个或一序列字符重复出现的次数。它们分别表示“没有或 更多”,“一次或更多”还有“没有或一次”。
         */
        String regex1 = "ab*";  //a后边有0个或者若干个b eg:a ab abbb abbbbb abbbbbbbbb...
        String regex2 = "ab+";  //ab+ 表示一个字符串有一个a后面跟着至少一个b或者更多
        String regex3 = "ab?";  //ab? 表示一个字符串有一个a后面跟着零个或者一个b;
        String regex4 = "a?b+$";  //a?b+$ 表示在字符串的末尾有零个或一个a跟着一个或几个b。
        String [] strs1 = {"a","abb","abbbb","cbbb","ba","aabb","cabsa","aaabbbcca"};
        for (String str : strs1) {
            if(str.matches(regex1)){
                System.out.print("1:"+str+"      ");
            }
            if(str.matches(regex2)){
                //System.out.print("2:"+str+"      ");  //可以逐个将注释打开了用以验证符合条件的字符串
            }
            if(str.matches(regex3)){
                //System.out.print("3:"+str+"      ");
            }
            if(str.matches(regex4)){
                //System.out.print("4:"+str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         * 用大括号括起,用以表示重复次数的范围  必须指定范围的下限
         * 当你测试完之后,可能注意到了,’*’,’+’和 ‘?’相当于”{0,}”,”{1,}”和”{0,1}”。 
         */
        String regex5 = "ab{2}";  //a后边有2个b eg:abb
        String regex6 = "ab{2,}";  //a后边最少有2个b eg:abb abbb abbbbb abbbbbb abbbbbbbbbbb
        String regex7 = "ab{2,5}";  //a后边有2到5个b eg:abb abbb abbbb abbbbb
        String [] strs2 = {"a","abb","abbbb","cbbb","ba","aabb","cabsa","aaabbbcca"};
        for (String str : strs2) {
            if(str.matches(regex5)){
                //System.out.print("5: "+str+"      ");
            }
            if(str.matches(regex6)){
                System.out.print("6: "+str+"      ");  //可以逐个将注释打开了用以验证符合条件的字符串
            }
            if(str.matches(regex7)){
                //System.out.print("7: "+str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         * “|” 代表或的意思    不知道为什么无法验证 原因还在查找中
         */
        String regex8 = "a|b";  //表示a或者b  eg:a b
        String regex9 = "(a|b)cd";  //a或者b字符后边带有cd eg:acd bcd
        String regex10 = "(a|b)*c";  //a或者b混合的字符串后边还带有c  eg:ac aac abc bac
        String [] strs3 = {"a","acc","bcc","cbbb","bac","aac","cabsa","aaabbbcca"};
        for (String str : strs3) {
            if(str.matches(regex8)){
                //System.out.print("8: "+str+"      ");
            }
            if(str.matches(regex9)){
                //System.out.print("9: "+str+"      ");  //可以逐个将注释打开了用以验证符合条件的字符串
            }
            if(str.matches(regex10)){
                System.out.print("10: "+str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         * “.” 可以代替任何字符  
         */
        String regex11 = "a.b";  //字符串中包含a后边有任意字符然后还有b  eg:a1b aab acb
        String regex12 = "a.[0-9]";  //包含a后边任意字符然后是单个数字  eg:ac1 ac2 a11 a22
        String regex13 = ".{3}";  //任意三个字符  eg:123 aac abc bac 1ac
        String [] strs4 = {"a1b","abb","a2bbbb","cbbb","ba","aabb","cabsa","aaabbbcca","a23","a11","a12121","123"};
        for (String str : strs4) {
            if(str.matches(regex11)){
                //System.out.print("11: "+str+"      ");
            }
            if(str.matches(regex12)){
                //System.out.print("12: "+str+"      ");  //可以逐个将注释打开了用以验证符合条件的字符串
            }
            if(str.matches(regex13)){
                System.out.print("13: "+str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         * “[]” 某些字符允许在一个字符串特定位置出现   
         */
        String regex14 = "[ab]";  //a或者b  eg:a b
        String regex15 = "[a-zA-Z]";  //匹配所有字母  eg:a b c d
        String regex16 = ".{3}";  //任意三个字符  eg:123 aac abc bac 1ac
        String [] strs5 = {"a","V","b","2","cbbb","ba","aabb","cabsa","aaabbbcca","a23","a11","a12121","123"};
        for (String str : strs5) {
            if(str.matches(regex14)){
                //System.out.print("14: "+str+"      ");
            }
            if(str.matches(regex15)){
                //System.out.print("15: "+str+"      ");  //可以逐个将注释打开了用以验证符合条件的字符串
            }
            if(str.matches(regex16)){
                System.out.print("16: "+str+"      ");
            }
        }
        System.out.println();
        System.out.println("------------------我是快乐的分界线------------------");
        
        /**
         * 接下来自己写一个匹配移动手机号码的正则表达式
         * 移动:139   138   137   136   135   134   147   150   151   152   157   158    159   178  182   183   184   187   188
         * 联通:130   131   132   155   156   185   186   145   176  
         * 电信:133   153   177   173   180   181   189 
         * 虚拟运营商:170  171 
         */
        String call = "(13[456789]|147|15[01278]|18[23478]|)[0-9]{8}";  //单独写一个移动的
        String s = "18801400309";
        boolean c = s.matches(call);
        System.out.println("这是对于移动手机号码的正则表达式验证:"+c);
        
        
        
    }

}
/**
     * 所有的正则表达式  在java程序中还需要加“\”进行转义
     * \s  空白符
     * . 除换行符以外的任意字符
     * () 匹配开始的位置 括号里的内容就是通过正则表达式想要获取的内容  eg:url=http://www.baidu.com  url=http://(.*) 获取到的就是www.baidu.com
     * \d 匹配数字  大写相反
     * \b 匹配单词的开始或者结束  大写相反
     * \w 匹配字母、数字、下划线、汉字  大写相反
     * ^ 字符串的开始
     * $ 字符串的结束
     * * 重复0次或者多次
     * + 重复1次或者多次
     * ? 重复0次或者1次
     * {n} 重复n次
     * {n,} 重复n次或更多次
     * {n,m} 重复n到m次
     * [^x] 除x以外的任意字符
     * [^aisd] 除aisd以外的任意字符
     * | 或者的意思
     */

 

以上栗子建议自己手敲一遍,能够增强理解。当你把这些基本的都敲了一遍之后就可以看看这个链接了。

正则表达式30分钟入门: http://deerchao.net/tutorials/regex/regex.htm 

posted @ 2018-05-14 13:13  背着泰山找黄河  阅读(234)  评论(0编辑  收藏  举报