package com.njwb18081.day13;
public class TestRegex {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
String str="aabbccddeeff";
boolean b=str.matches("aabbccddeeff");//matches(String/regex格式)
System.out.println(b);
String regex="[abcdef]";//只包含a,b,c,d,e,f单个字符的正则
System.out.println("z".matches(regex));//false
System.out.println("f".matches(regex));//true
// 除了abc字符的正则
System.out.println("f".matches("[^abc]"));//true
System.out.println("a".matches("[^abc]"));//false
// 大写A~D G~Z的正则
System.out.println("E".matches("[A-D[G-Z]]"));//false
System.out.println("H".matches("[A-D[G-Z]]"));//true
// 数字出现0次或一次
System.out.println("66".matches("[0-9]?"));//false
// 数字出现0次或多次
System.out.println("66".matches("[0-9]*"));//true
// 大写A~Z英文字符最少4个最多6个
System.out.println("AAAAAAA".matches("[A-Z]{4,6}"));
// java中针对于字符串反斜杠\通过\做转移
// 数字恰好是5次后跟小写英文字母恰好是5次
System.out.println("12321adsad".matches("(\\d){5}([a-z]{5})"));
// QQ号 6位~11位 \d{6,11}
System.out.println("1234567890".matches("\\d{6,11}"));
// QQ邮箱 数字6~11位@qq.com
System.out.println("1234567890@qq.com".matches("\\d{6,11}@qq.com"));
// 邮箱格式 数字6~11位@qq或sina.com或cn \\d{6,11}@(qq|sina).(com|cn)
// 1234567890@qq.com
// 1234567890@sina.com
// 1234567890@qq.cn
// 1234567890@sina.cn
System.out.println("1234567890@sina.cn".matches("\\d{6,11}@(qq|sina).(com|cn)"));
// 电话号码 3~4位的数字-8位数字 \\d{3,4}-\d{8}
System.out.println("0536-12345678".matches("\\d{3,4}-\\d{8}"));
// 密码 首字母大写后跟8~11位英文或数字@qq或sina.com或cn
// [A-Z]{1}(\\d|[a-zA-Z]) {8,11}@(qq|sina).(com|cn)
System.out.println("LaaaAAA11134@sina.com".matches("[A-Z]{1}(\\d|[a-zA-Z]){8,11}@(qq|sina).(com|cn)"));
String str2="sadsa12321sadsad213213sadsad32132";
System.out.println(str2.replaceAll("\\d{5,6}", "HELLO"));
str2="13955663344,13955664544,13955668844,13955665644";
System.out.println(str2.replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$3"));
}
}
TestRegex
最新推荐文章于 2022-12-14 22:30:27 发布