java 正则表达式基础练习
package com.reviewquestions.review_questions.javaBase;
public class MatcherTest {
public static boolean matcherMethod(String pattern,int mode){
if (pattern == null){
mode = 99999;
}
boolean matches = false;
switch(mode){
case 1 ://1、求非负整数 :
return matches = pattern.matches("^\\d+$");
case 2 ://2、匹配正整数:
return matches = pattern.matches("^[1-9][0-9]*$");
case 3 ://3、非正整数:
return matches = pattern.matches("^-[1-9][0-9]*|-0$");
case 4 ://4、负整数:
return matches = pattern.matches("^-[1-9][0-9]*$");
case 5 ://5、整数 :
return matches = pattern.matches("^[-|+]?(0|[1-9][0-9]*)$");
case 6 ://6、非负浮点数 :
return matches = pattern.matches("^[+]?\\d+\\.\\d+$");
case 7 ://7、正浮点数 有限的情况 00.001不合规范的数也会被匹配
return matches = pattern.matches("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
case 8 ://8、非正浮点数
return matches = pattern.matches("^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$");
case 9 ://9、浮点数
return matches = pattern.matches("^(-?\\d+)(\\.\\d+)?$");
case 10 ://10、有数字、26个英文字母组成的字符串
return matches = pattern.matches("^[A-Za-z0-9]+$");
case 11 ://11、验证输入只能是汉字 :
return matches = pattern.matches("^[\\u4e00-\\u9fa5]{0,}$");
default :
System.out.println("请输入正确的参数");
return false;
}
}
public static void main(String[] args) {
boolean result = MatcherTest.matcherMethod("宿", 11);
System.out.println(result);
}
}