Java正则表达式小测试

public class RegularTest {


    public static void main(String[] args) {

        //非贪心获取
        String feifei = "feifei is 123 a 45 dog!";
        Pattern pattern = Pattern.compile("\\d+?", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(feifei);
        while (matcher.find()){
           //加问号-非非贪心获取  1,2,3,4,5
           //不加问号 123,45
           System.out.print(matcher.group()+ ",");
        }

        System.out.println("");
        //获取某一段匹配数据
        String dateStr = "feifei 2013-02-14 is a dog!";
        Pattern p1 = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher m1 = p1.matcher(dateStr);
        while (m1.find()){
            //2013-02-14
            System.out.print(m1.group()+ ",");
        }

        System.out.println("");
        //获取连续出现(重复)单词
        String feiStr = "feifeifei is aa a dog! feifei fei";
        Pattern p2 = Pattern.compile("(fei)\\1+");
        Matcher m2 = p2.matcher(feiStr);
        while (m2.find()){
            //feifeifei,feifei--获取连续出现fei的,
            System.out.print(m2.group()+ ",");
        }
        System.out.println("");


        String window2 = "Window2000 Window3.1 Window98 ";
        Pattern p4 = Pattern.compile("Window(?:2000|98)");
        Matcher m4 = p4.matcher(window2);
        while (m4.find()) {
            //Window2000,Window98----
            System.out.print(m4.group() + ",");
        }
        System.out.println("");


        //正向预查
        String window = "Window2000 Window3.1 Window98 ";
        Pattern p3 = Pattern.compile("Window(?=2000|98)");
        Matcher m3 = p3.matcher(window);
        while (m3.find()) {
            //Window,Window----获得的是Window2000,Window98 中的Window,
            System.out.print(m3.group() + ",");
        }

        System.out.println("");


        //捕获组
        String group = "[email protected]";
        Pattern p5 = Pattern.compile("(\\w+)@([\\w\\.]+)");
        Matcher m5 = p5.matcher(group);
        while (m5.find()) {
            System.out.print(m5.group() + ",");
        }
        m5.matches();
        System.out.print(m5.group(0) + ",");
        System.out.print(m5.group(1) + ",");
        System.out.print(m5.group(2) + ",");
        //[email protected], [email protected], feifei, 163.com
        /*表达式 (A)(B(C)) 中,存在四个这样的组: (A)(B(C)),(A), (B(C)), (C) */

        System.out.println("");

        //捕获组替换字符
        System.out.println("1b23".replaceFirst("b(2)","$1a"));//12a3

    }

 

 

下面是杂乱的东西,

private id number(11, 0);   //  ([A-Za-z0-9_]+) .*;



 

你可能感兴趣的:(Java正则表达式小测试)