java 正则表达式:matches()


 

public class Demo04 { public static void main(String[] args) { //匹配正则表达式:matches //校验qq号码 //1: 要求必须是5-15位数字 //2: 0不能开头 //描述规则
        String regex="[1-9][0-9]{4,14}"; //描述qq号
        String qq="947277425"; //判断
        boolean flag=qq.matches(regex); System.out.println(flag); //校验手机号 //1:要求为11位数字 //2:第1位为1,第2位为3、4、5、7、8中的一个,后面9位为0到9之间的任意数字。 //描述规则
        String r="[1][34567][0-9]{9}"; //描述手机号
        String phone="13964829654"; //判断
        boolean f=phone.matches(r); System.out.println(f); //split()分割
        String str="861-139-3646-4564"; String regex="-"; String [] arr=str.split(regex); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } //替换
        String stt="as5fdsfdsfds5f48fdsf564fds"; String re="[0-9]+"; String s=stt.replaceAll(re, "*"); System.out.println(s); //匹配邮箱
        String reg="[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-z]+)+"; String em="kemi7812jijl@126.com"; boolean flag=em.matches(reg); System.out.println(flag); } }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM