java中常见的正则表达式

java中常见的正则表达式(更细节的表达式讲解在主页):
package com.feisi.test;
public class Test8 {
public static void main(String[] args) {

    //1、匹配汉字:^[\\u4e00-\\u9fa5]{0,3}$
    String str1 = "你好吗";
    boolean b1 = str1.matches("^[\\u4e00-\\u9fa5]{0,3}$");
    System.out.println(b1);//true

    //2. 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
    //任选一种
    String str2 = "cvhds15622";
    boolean b2 = str2.matches("^[A-Za-z0-9]+$");
    System.out.println(b2);//true

    // 3.Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
    String str3 = "1581632569@11.com";
    boolean b3 = str3.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    System.out.println(b3);//true

    //4.电话号码:^(\d{3,4})-\d{7,8}$
    String str4 = "199-73994752";
    boolean b4 = str4.matches("^(\\d{3,4})-\\d{7,8}$");
    System.out.println(b4);//true

    //5.手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
    //任选一种方式
    String str5 = "18873884752";
    boolean b5 = str5.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
    System.out.println(b5);//true

    //6. 腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始)
    String str6 = "1887388";
    boolean b6 = str6.matches("[1-9][0-9]{4,}");
    System.out.println(b6);//true

   // 7. 日期格式:^\d{4}-\d{1,2}-\d{1,2}
    String str7 = "1999-10-24";
    boolean b7 = str7.matches("^\\d{4}-\\d{1,2}-\\d{1,2}");
    System.out.println(b7);//true

    //8. 身份证号(15位、18位数字):^\d{15}|\d{18}$
    String str8 = "575382325381235";
    boolean b8 = str8.matches("^\\d{15}|\\d{18}$");
    System.out.println(b8);//true

    //9. 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
    String str9 = "shi_1024";
    boolean b9 = str9.matches("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
    System.out.println(b9);//true

    //10. 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$
    String str10 = "shi_1024";
    boolean b10 = str10.matches("^[a-zA-Z][a-zA-z0-9_]{5,17}$");
    System.out.println(b10);//true
    
    //11. 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
    String str11 = "Shi_1024";
    boolean b11 = str11.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
    System.out.println(b11);//true
}

}

最后附上史上最全的正则表达式:
https://www.cnblogs.com/fozero/p/7868687.html

//作业:登录界面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值