java判断字符串是否全部由数字,大小写字母,特殊符号组成

时间:2022-12-16 09:12:10

直接上代码,经过验证。

package javaTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {

	/***
	 * 
	 * @param args
	 * java判断字符是否是全部由数字,小写字母,大写字母,特殊符号组成
	 * 正则表达式判断
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "aksjfjJAJIFJSL12455432^&^&(";
		String str1 = "abcdfjijgiaj";
		String str2 = "JAOGVNDIJEIO";
		String str3 = "12345465870";
		String str4 = "@#$^^%^&*(";
		
		
		Pattern pattern1 = Pattern.compile("[a-z]*");
		Pattern pattern2 = Pattern.compile("[A-Z]*");
		Pattern pattern3 = Pattern.compile("[0-9]*");
		Pattern pattern4 = Pattern.compile("\\p{Punct}+");
		
		Matcher matcher1 = pattern1.matcher(str1);
		Matcher matcher2 = pattern2.matcher(str2);
		Matcher matcher3 = pattern3.matcher(str3);
		Matcher matcher4 = pattern4.matcher(str4);
		
		
		if (matcher1.matches()) {
			System.out.println("全是小写字母");
		}
		else {
			System.out.println("不全是小写字母");
		}
		
		
		if (matcher2.matches()) {
			System.out.println("全是大写字母");
		}
		else {
			System.out.println("不全是大写字母");
		}
		
		
		if (matcher3.matches()) {
			System.out.println("全是数字");
		}
		else {
			System.out.println("不全是数字");
		}
		
		
		if (matcher4.matches()) {
			System.out.println("全是特殊符号");
		}
		else {
			System.out.println("不全是特殊符号");
		}
		
		
		//判断字符串是否全部由数字组成,java自带方法判断
		boolean flag = true;
		for (int i = str3.length()-1; i >= 0; i--) {
			if (!Character.isDigit(str3.charAt(i))) {
				flag = false;
			}
		}
		if (flag) {
			System.out.println(str3 + "全部是数字");
		}

	}

}