正则表达式

正则表达式

正则表达式:
	定义:正确规则的表达式,他是一种独立的语法,很多语言都支持;
	作用:就是校验数据,符不符合定义的这个正则表达式。

案例:
	需求:检验QQ号是否合理
		1: 要求必须是5 - 15 位数字
        2: 0 不能开头
public static void main(String[] args) {
  		Scanner sc = new Scanner(System.in);
        System.out.println("请输入的你的QQ号");
        String qqNumber = sc.nextLine();
        //调用校验的方法
        boolean flag = checkQQNumber2(qqNumber);
        if (flag) {
            System.out.println("QQ号规则正确");
        } else {
            System.out.println("QQ号规则错误");
        }

    }
private static boolean checkQQNumber2(String qqNumber) {
        //定义正则表达式
        String regx = "[1-9][0-9]{4,14}";
        boolean b = qqNumber.matches(regx);
        return b;
    }
    
正则表达式定义:
 String regx = "a";		//只是a这个字母
        regx = "[a,b,c]"; //是列举的小写字母的某一个
        regx = "[a-z]";	  //单个字符是小写字母
        regx = "[A,B,C,D]";	//列举的几个大写字母的某一个
        regx = "[A-Z]";		//任意大写字母的某一个
        regx = "[a-zA-Z]";	//任意大写或者小写字母的某一个
        regx = "[0,1,2,3]";	//列举的数字中的某一个
        regx = "[0-9]";		//匹配一个任意数字字符
        regx = "[a-zA-Z0-9]";	//匹配一个小写字母或者大写字母或者数字
        regx = "[^0-9]"; //^不是列表中的某一个
        regx = ".";//配置单个任意字符。
        regx = "\\.";   // \ 转义字符
        regx = "|"; // | 或者
        regx = "\\|"; //转义为'|'
        regx = "\\d"; // 跟 [0-9] 这个的意思一样   \D 非数字: [^0 - 9]
        regx = "\\w"; // 跟 [a-zA-Z_0-9] 这个意思一样  \W 非单词字符:[^\w]
        regx = " ";		
        regx = "\\s"; //空格  \S 非空白字符:[^\s]
        regx = "abc$"; //以abc结尾
        regx = "^abc"; // 以abc开头
        regx = "[0-9]+"; //0-9 + 可以出现一个或多个
        regx = "[a-z]*"; //0-9 * 0个或多个 1个也算多个  空串算0个
        regx = "[A-Z]?"; // ? 一个或 0个
        regx = "\\w+";
        regx = "[a-zA-Z0-9_]+";
        regx = "[a-z]{6}"; //正好几个
        regx = "[a-zA-Z]{6,}"; //至少6个
        regx = "[a-zA-Z]{6,16}"; //大于等于6 小于等于16
正则表达式常用案例:
//matches(phoneRegx); 判断这个字符串,是否符合传入的这个正则表达式。
  //通用版的邮箱规则
        emailRegx = "[a-zA-Z]\\w{5,17}@[1-9a-z]{3,10}\\.(com|cn|net|org)";

        boolean matches = "westos@163.net".matches(emailRegx);
        System.out.println(matches);

  //校验中文的正则。
        String regx="[\\u4e00-\\u9fa5]{2,12}";
        System.out.println("王老虎".matches(regx));

   //校验身份证的正则
   //[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]
   String shenFen="[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]";

    //车牌号码的正则
        String car="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}";

根据正则来切割字符串
//根据正则来切割这个字符串,返回的是一个字符串数组。
	 public static void main(String[] args) {
        String names="张三=李四=王五";
        //正则表达式:split("="),使用"="分割字符串
        String[] strings = names.split("=");
        System.out.println(strings[0]);	//张三
        System.out.println(strings[1]);	//李四
        System.out.println(strings[2]);	//王五
    }
正则表达式的替换功能
根据正则表达式(regex),将字符串不符合的正则的内容替换为指定内容(replecement);
String类的功能:public String replaceAll (String regex, String replacement);
案例:
public static void main(String[] args) {
       //保留字符串中的中文字符
       String str2 = "abc你好abc";
       //根据正则表达式去替换
       String s = str.replaceAll("[^\\u4e00-\\u9fa5]", "");
       System.out.println(s);	//你好
	}
正则表达式的查找和获取功能
boolean find ();尝试查找与该模式匹配的输入序列的下一个子序列。
String group ();返回由以前匹配操作所匹配的输入子序列。

案例: 
	//获取下面字符串中 是三个字母组成的单词。
	public static void main(String[] args) {
	String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
	   String regx="\\b[a-z]{3}\\b";

       Pattern pattern = Pattern.compile(regx);	//模式器
       Matcher matcher = pattern.matcher(str);  //匹配器
        while (matcher.find()) {
            String s = matcher.group();
            System.out.println(s);
        }
    }
正则的模式器和匹配器
//匹配器Matche   模式器Pattern
//把一个正则表达式封装到模式器里面
Pattern pattern = Pattern.compile(regx);//把正则表达式regex封装在模式器中;
Matcher matcher = pattern.matcher(str); //  //调用模式器中的matcher(Str)方法,传入一个待匹配的数据(Str),返回一个匹配器;
案例:
	 public static void main(String[] args) {
        //把一个正则表达式("a*b")封装到模式器里面
        Pattern p = Pattern.compile("a*b");
        //调用模式器中的matcher("aaaaab");方法,传入一个待匹配的数据,返回一个匹配器
        Matcher m = p.matcher("aaaaab");
        //调用匹配器中的匹配的方法,看数据是否匹配正则
        boolean b = m.matches();
        System.out.println(b);
        //如果你的需求只是看一个数据符不符合一个正则,你只需要调用,String类中的matches("a*b")
        System.out.println("aaaaab".matches("a*b"));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值