![]()
package RegularExpression.regex;
/**
* FileName: regularExpression01
* Author: lps
* Date: 2022/4/13 12:59
* Sign:刘品水 Q:1944900433
*/
public class regularExpression01 {
public static void main(String[] args) {
//需求 校验qq号 必须全部数字6-20位
// Scanner sc = new Scanner(System.in);
// String input = sc.next();
// System.out.println(checkQQ(input));
System.out.println(checkQQ(null));//false
System.out.println("========");
//正则表达式的初体验
System.out.println(checkQQ2("20201425"));//true
System.out.println(checkQQ2("qwertyuiop"));//false
}
public static boolean checkQQ2(String qq){
return qq!=null&&qq.matches("\\d{6,20}");
}
public static boolean checkQQ(String qq) {
//判断qq号码的长度是否满足要求
if (qq == null || qq.length() < 6 || qq.length() > 20) {
return false;
}
//判断qq号码是否全是数字 不然返回false
//24262a226
for (int i = 0; i < qq.length(); i++) {
//获取每一个字符
//char charAt(int index) 返回指定索引处的 char值。
char ch = qq.charAt(i);
//判断字符是否不是数字 不是数字直接返回false
//利用ascll码来
if (ch < '0' || ch > '9') {
return false;
}
}
return true;
}
}
package RegularExpression.regex;
/**
* FileName: regexDemo01
* Author: lps
* Date: 2022/4/13 13:33
* Sign:刘品水 Q:1944900433
*/
public class regexDemo01 {
public static void main(String[] args) {
//public boolean matches(String regex):判断是否正则表达式匹配
//boolean matches(String regex) 告诉这个字符串是否匹配给定的 regular expression 。
//只能单一a、b、c
System.out.println("a".matches("[abc]"));//true
System.out.println("b".matches("[abc]"));//true
//不能出现abc||adc
System.out.println("a".matches("[^abc]"));//false
System.out.println("d".matches("[^adc]"));//false
System.out.println("d".matches("\\d"));//false
System.out.println("1".matches("\\d"));//true
System.out.println("1944900433".matches("\\d"));//false
System.out.println("1944900433".matches("\\d{6,20}"));//true
System.out.println("======");
System.out.println("z".matches("\\w"));//true
System.out.println("12".matches("\\w"));//false
System.out.println("12616".matches("\\w{1,10}"));//true
System.out.println("你好".matches("\\w{1,10}"));//false
System.out.println("你好".matches("\\W"));//false
System.out.println("你好".matches("\\W{1,10}"));//true
System.out.println("=================");
System.out.println("lps".matches("\\w{6,}"));//false
System.out.println("lps123456".matches("\\w{6,}"));//true
//验证码必须是四位 字母数字且不包括下划线
System.out.println("12qw".matches("[\\w&& [^_]]{4}"));//true
System.out.println("12_w".matches("[\\w&& [^_]]{4}"));//false
System.out.println("12qw".matches("\\w&&[^_]{4}"));//false 外面的[]不要少!!!
System.out.println("23df".matches("[a-zA-Z0-9]{4}"));//true
System.out.println("23_a".matches("[a-zA-Z0-9]{4}"));//false
}
}
![]()