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 = "feifei@163.com";
        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) + ",");
        //feifei@163.com, feifei@163.com, 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_]+) .*;



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值