java正则表达式

新接触Java时间不长,学一点用一点。
这两天涉及到在句子中选择关键词,进行分门别类。开始使用的是contains,后来发现当同时包含多个关键词时,无法表示出关键词的先后顺序,所以自学了一点正则表达式。
            String temporary = "123";
            temporary.matches("^[0-9]*$");
            temporary = "abc";
            temporary.matches("^[a-z]*$");
            temporary = "Abc";
            temporary.matches("^[A-z]*$");
            temporary = "联系不上";//判断test和teacher同时存在
            String pattern = "联系.?.不.*";
            boolean isMatch = Pattern.matches(pattern,temporary);

            String str = "this is text";
            String regEx = "this\\s+is\\s+text";
            isMatch = Pattern.matches(regEx,str);
            System.out.println(isMatch);
//            regEx = "(^[a-zA-Z]+$)+this\\s+is\\s+text";
            str = "thisistext";
            regEx = "^[a-zA-Z]+$";
            isMatch = Pattern.matches(regEx,str);
            System.out.println(isMatch);

            str = "thisis text";
            regEx = "^[a-zA-Z]+\\s+[a-zA-Z]+";
            isMatch = Pattern.matches(regEx,str);
            System.out.println(isMatch);

            str = "你好中国";
            regEx = "[\\u0391-\\uFFE5]+";//[a-zA-Z0-9-\u0391-\uFFE5]+ 匹配中文 字母 数字 不匹配一些奇怪的标点符号
            isMatch = Pattern.matches(regEx,str);
            System.out.println(isMatch);

            String string="测试<>《》  !*(^)$%~!@#$…&%¥—+=、。,;‘’“”:·`文本";

            System.out.println(string.replaceAll("\\pP|\\pS", ""));//去掉标点符号,不管空格
            str = string.replaceAll("\\pP|\\pS", "");//去掉标点符号,不管空格
            //str =  str.replaceAll(" ", "");

            regEx = "[\\u0391-\\uFFE5]+\\s+[\\u0391-\\uFFE5]+";//[a-zA-Z0-9-\u0391-\uFFE5]+ 匹配中文 字母 数字 不匹配一些奇怪的标点符号
            isMatch = Pattern.matches(regEx,str);
            System.out.println(isMatch);

            str = "哎呀,在搞什么锤子啊你";
            System.out.println(str.matches(".*((你.*锤子)|(锤子.*你)).*"));//匹配带有你和锤子的句子 你在前|锤子在前

            str = "2017年12月12日15:19:44朋友帮忙尝试联系";
            isMatch = str.matches(".*(帮忙.*联系).*");
            System.out.println(isMatch);

            str = "客户新买号码1个月左右";
            regEx = ".*(\\d{2})\\号.*";
            isMatch = str.matches(regEx);
            System.out.println(isMatch);

            str = "22号之前处理................";

            isMatch = str.matches(regEx);
            System.out.println(isMatch);

            String REGEX_MOBILE = ".*(((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}).*";
            str = "配偶裘君13777255504接你电话说了问一下客户";
            isMatch = str.matches(REGEX_MOBILE);
            System.out.println(isMatch);

            String datetime = "0915年02月22日你好啊";
            Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))" + "[\\年\\/\\s]?((" +
                    "((0?[13578])|(1[02]))" + "[\\月\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01]))[\\日\\/\\s]?)" + // 31day
                    "|(((0?[469])|(11))[\\月\\/\\s]?((0?[1-9])|([1-2][0-9])|(30))[\\日\\/\\s]?)" + // 30day
                    "|(0?2[\\月\\/\\s]?((0?[1-9])|([1-2][0-9]))[\\日\\/\\s]?)))" + // 闰年 2月有29天
                    "|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\年\\/\\s]?((" +
                    "((0?[13578])|(1[02]))[\\月\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01]))[\\日\\/\\s]?)" +
                    "|(((0?[469])|(11))[\\月\\/\\s]?((0?[1-9])|([1-2][0-9])|(30))[\\日\\/\\s]?)" +
                    "|(0?2[\\月\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))[\\日\\/\\s]?))))" +// 非闰年 2月只有28天  // 日期 年 月 日 jldsajlfajla
                    "(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?.*$");// 时间HH:MM:SS
            isMatch = p.matcher(datetime).matches();
            System.out.println(isMatch);


仅作记录, .*表示可有任何字符
                ?表示可有零次或一次
                \\s表示可有任意空格
                ^表示开始
                $表示结束


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值