关于java程序中的正则表达式的应用小解(1)

来源:互联网 发布:用阿里云gpu挖比特币 编辑:程序博客网 时间:2024/05/18 20:37

****最近在开发一个小型的java与mysql的小系统,用到了正则表达式


        String patternStr1 = "[0-9a-zA-Z]{6,12}";       //字符串格式为6~12位的字母或者数字

        String patternStr2 = "[0-9]*"                              //字符串格式只能为数字

        String patternStr3 = "[0-9]{12}";                 //字符串必须为12位数字

        String targetString = "     "              //此处为要配对的字符串

        targetString.matches(patternStr);     //此处返回true or false



更新②:

注:判断100以内的数,只能有一位小数

//分四种不同的情况
String pattern1 = "[0-9]{2}";
String pattern2 = "[0-9]{2}+\\.[0-9]{1}";
String pattern3 = "[0-9]{1}";
String pattern4 = "[0-9]{1}+\\.[0-9]{1}";
if (str.matches(pattern1) ) {
return true;
}
if(str.matches(pattern2)){
return true;
}
if (str.matches(pattern3) ) {
return true;
}
if(str.matches(pattern4)){
return true;
}

0 0