Java专题技术:Java-最完整正则表达式验证
Posted 尚观云科技
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java专题技术:Java-最完整正则表达式验证相关的知识,希望对你有一定的参考价值。
package com.lyqc.utils;
import org.apache.commons.lang.StringUtils;
public class ValidUtils {
/**手机号*/
private static final String mobile = "^(13|15|18|17|16)[0-9]{9}$";
private static final String codeAndMobile = "^\+[0-9]{2}\-(13|15|18|17|16)[0-9]{9}$";
/**整数*/
private static final String intege="^-?[1-9]\d*$";
/** 正整数*/
private static final String intege1="^[1-9]\d*$";
/** 负整数*/
private static final String intege2="^-[1-9]\d*$";
/** 数字*/
private static final String num="^([+-]?)\d*\.?\d+$";
/** 正数(正整数 + 0)*/
private static final String num1="^[1-9]\d*|0$";
/** 负数(负整数 + 0)*/
private static final String num2="^-[1-9]\d*|0$";
/** 浮点数*/
private static final String decmal="^([+-]?)\d*\.\d+$";
/** 正浮点数*/
private static final String decmal1="^[1-9]\d*.\d*|0.\d*[1-9]\d*$";
/** 负浮点数*/
private static final String decmal2="^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$";
/** 浮点数*/
private static final String decmal3="^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0)$";
/** 非负浮点数(正浮点数 + 0)*/
private static final String decmal4="^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$";
/**非正浮点数(负浮点数 + 0) */
private static final String decmal5="^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0$";
/** 邮件*/
private static final String email="^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
/** 颜色*/
private static final String color="^[a-fA-F0-9]{6}$";
/** url*/
private static final String url="^http[s]?=\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$";
/** 仅中文*/
private static final String chinese="^[\一-\龥\豈-\鶴]+$";
/** 仅ACSII字符*/
private static final String ascii="^[\-\ÿ]+$";
/**邮编 */
private static final String zipcode="^\d{6}$";
private static final String ip4="^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$";
/** 非空*/
private static final String notempty="^\S+$";
/**图片 */
private static final String picture="(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
/** 压缩文件*/
private static final String rar="(.*)\.(rar|zip|7zip|tgz)$";
/** 日期*/
private static final String date="^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$";
/** QQ号码*/
private static final String qq="^[1-9]*[1-9][0-9]*$";
private static final String tel="^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{1,}))?$";
/** 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串*/
private static final String username="^\w+$";
/** 字母*/
private static final String letter="^[A-Za-z]+$";
private static final String letterAndSpace="^[A-Za-z ]+$";
/** 大写字母*/
private static final String letter_u="^[A-Z]+$";
/** 小写字母*/
private static final String letter_l="^[a-z]+$";
/** 身份证*/
private static final String idcard="^[1-9]([0-9]{14}|[0-9]{17})$";
/**判断字符串是否为浮点数*/
private static final String isFloat="^[-]?\d+(\.\d+)?$";
/**判断字符串是否为正浮点数*/
private static final String isUFloat="^\d+(\.\d+)?$";
/**判断是否是整数*/
private static final String isInteger="^[-]?\d+$";
/**判断是否是正整数*/
private static final String isUInteger="^\d+$";
/**判断车辆Vin码*/
private static final String isCarVin="^[1234567890WERTYUPASDFGHJKLZXCVBNM]{13}[0-9]{4}$";
/** 手机号 */
public static boolean isMobile(String input){
return matches(mobile, input);
}
public static boolean isCodeAndMobile(String input){
return matches(codeAndMobile, input);
}
/** 整数 */
public static boolean isIntege(String input){
return matches(intege, input);
}
/** 正整数 */
public static boolean isintege1(String input){
return matches(intege1, input);
}
/** 负整数*/
public static boolean isIntege2(String input){
return matches(intege2, input);
}
/** 数字 */
public static boolean isNum(String input){
return matches(num, input);
}
/** 正数(正整数 + 0) */
public static boolean isNum1(String input){
return matches(num1, input);
}
/** 负数(负整数 + 0)*/
public static boolean isNum2(String input){
return matches(num2, input);
}
/** 浮点数*/
public static boolean isDecmal(String input){
return matches(decmal, input);
}
/** 正浮点数*/
public static boolean isDecmal1(String input){
return matches(decmal1, input);
}
/** 负浮点数*/
public static boolean isDecmal2(String input){
return matches(decmal2, input);
}
/**浮点数 */
public static boolean isDecmal3(String input){
return matches(decmal3, input);
}
/** 非负浮点数(正浮点数 + 0)*/
public static boolean isDecmal4(String input){
return matches(decmal4, input);
}
/** 非正浮点数(负浮点数 + 0)*/
public static boolean isDecmal5(String input){
return matches(decmal5, input);
}
/** 邮件*/
return matches(email, input);
}
/** 颜色*/
public static boolean isColor(String input){
return matches(color, input);
}
/** url*/
public static boolean isUrl(String input){
return matches(url, input);
}
/** 中文*/
public static boolean isChinese(String input){
return matches(chinese, input);
}
/** ascii码*/
public static boolean isAscii(String input){
return matches(ascii, input);
}
/** 邮编*/
public static boolean isZipcode(String input){
return matches(zipcode, input);
}
public static boolean isIP4(String input){
return matches(ip4, input);
}
/** 非空*/
public static boolean isNotEmpty(String input){
return matches(notempty, input);
}
/** 图片*/
public static boolean isPicture(String input){
return matches(picture, input);
}
/** 压缩文件*/
public static boolean isRar(String input){
return matches(rar, input);
}
/** 日期*/
public static boolean isDate(String input){
return matches(date, input);
}
/** qq*/
public static boolean isQQ(String input){
return matches(qq, input);
}
public static boolean isTel(String input){
return matches(tel, input);
}
/** 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串*/
public static boolean isUserName(String input){
return matches(username, input);
}
/**字母*/
public static boolean isLetter(String input){
return matches(letter, input);
}
public static boolean isLetterAndSpace(String input){
return matches(letterAndSpace, input);
}
/** 小写字母*/
public static boolean isLowLetter(String input){
return matches(letter_l, input);
}
/** 大写字母*/
public static boolean isUpperLetter(String input){
return matches(letter_u, input);
}
/** 身份证*/
public static boolean isIDCard(String input){
return matches(idcard, input);
}
public static boolean isFloat(String input){
return matches(isFloat, input);
}
public static boolean isUFloat(String input){
return matches(isUFloat, input);
}
public static boolean isInteger(String input){
return matches(isInteger, input);
}
public static boolean isUInteger(String input){
return matches(isUInteger, input);
}
public static boolean isCarVin(String carVin){
return matches(isCarVin, carVin);
}
public static boolean matches(String regex,String input){
if(StringUtils.isBlank(input)) return false;
if(input.matches(regex))return true;
return false;
}
public static void main(String[] args) {
/*System.out.println(isInteger("1"));
System.out.println(isInteger("0"));
System.out.println(isInteger("-1"));
System.out.println(isInteger("1.0"));
System.out.println("--------------------");
System.out.println(isUInteger("-1"));
System.out.println(isUInteger("0"));
System.out.println(isUInteger("10"));
System.out.println(isUInteger("1.3"));*/
System.out.println(isLetterAndSpace("tai wan"));
}
}
package com.lyqc.utils;
import org.apache.commons.lang.StringUtils;
public class ValidUtils {
/**手机号*/
private static final String mobile = "^(13|15|18|17|16)[0-9]{9}$";
private static final String codeAndMobile = "^\+[0-9]{2}\-(13|15|18|17|16)[0-9]{9}$";
/**整数*/
private static final String intege="^-?[1-9]\d*$";
/** 正整数*/
private static final String intege1="^[1-9]\d*$";
/** 负整数*/
private static final String intege2="^-[1-9]\d*$";
/** 数字*/
private static final String num="^([+-]?)\d*\.?\d+$";
/** 正数(正整数 + 0)*/
private static final String num1="^[1-9]\d*|0$";
/** 负数(负整数 + 0)*/
private static final String num2="^-[1-9]\d*|0$";
/** 浮点数*/
private static final String decmal="^([+-]?)\d*\.\d+$";
/** 正浮点数*/
private static final String decmal1="^[1-9]\d*.\d*|0.\d*[1-9]\d*$";
/** 负浮点数*/
private static final String decmal2="^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$";
/** 浮点数*/
private static final String decmal3="^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0)$";
/** 非负浮点数(正浮点数 + 0)*/
private static final String decmal4="^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$";
/**非正浮点数(负浮点数 + 0) */
private static final String decmal5="^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0$";
/** 邮件*/
private static final String email="^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
/** 颜色*/
private static final String color="^[a-fA-F0-9]{6}$";
/** url*/
private static final String url="^http[s]?=\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$";
/** 仅中文*/
private static final String chinese="^[\一-\龥\豈-\鶴]+$";
/** 仅ACSII字符*/
private static final String ascii="^[\-\ÿ]+$";
/**邮编 */
private static final String zipcode="^\d{6}$";
private static final String ip4="^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$";
/** 非空*/
private static final String notempty="^\S+$";
/**图片 */
private static final String picture="(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
/** 压缩文件*/
private static final String rar="(.*)\.(rar|zip|7zip|tgz)$";
/** 日期*/
private static final String date="^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$";
/** QQ号码*/
private static final String qq="^[1-9]*[1-9][0-9]*$";
private static final String tel="^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{1,}))?$";
/** 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串*/
private static final String username="^\w+$";
/** 字母*/
private static final String letter="^[A-Za-z]+$";
private static final String letterAndSpace="^[A-Za-z ]+$";
/** 大写字母*/
private static final String letter_u="^[A-Z]+$";
/** 小写字母*/
private static final String letter_l="^[a-z]+$";
/** 身份证*/
private static final String idcard="^[1-9]([0-9]{14}|[0-9]{17})$";
/**判断字符串是否为浮点数*/
private static final String isFloat="^[-]?\d+(\.\d+)?$";
/**判断字符串是否为正浮点数*/
private static final String isUFloat="^\d+(\.\d+)?$";
/**判断是否是整数*/
private static final String isInteger="^[-]?\d+$";
/**判断是否是正整数*/
private static final String isUInteger="^\d+$";
/**判断车辆Vin码*/
private static final String isCarVin="^[1234567890WERTYUPASDFGHJKLZXCVBNM]{13}[0-9]{4}$";
/** 手机号 */
public static boolean isMobile(String input){
return matches(mobile, input);
}
public static boolean isCodeAndMobile(String input){
return matches(codeAndMobile, input);
}
/** 整数 */
public static boolean isIntege(String input){
return matches(intege, input);
}
/** 正整数 */
public static boolean isintege1(String input){
return matches(intege1, input);
}
/** 负整数*/
public static boolean isIntege2(String input){
return matches(intege2, input);
}
/** 数字 */
public static boolean isNum(String input){
return matches(num, input);
}
/** 正数(正整数 + 0) */
public static boolean isNum1(String input){
return matches(num1, input);
}
/** 负数(负整数 + 0)*/
public static boolean isNum2(String input){
return matches(num2, input);
}
/** 浮点数*/
public static boolean isDecmal(String input){
return matches(decmal, input);
}
/** 正浮点数*/
public static boolean isDecmal1(String input){
return matches(decmal1, input);
}
/** 负浮点数*/
public static boolean isDecmal2(String input){
return matches(decmal2, input);
}
/**浮点数 */
public static boolean isDecmal3(String input){
return matches(decmal3, input);
}
/** 非负浮点数(正浮点数 + 0)*/
public static boolean isDecmal4(String input){
return matches(decmal4, input);
}
/** 非正浮点数(负浮点数 + 0)*/
public static boolean isDecmal5(String input){
return matches(decmal5, input);
}
/** 邮件*/
return matches(email, input);
}
/** 颜色*/
public static boolean isColor(String input){
return matches(color, input);
}
/** url*/
public static boolean isUrl(String input){
return matches(url, input);
}
/** 中文*/
public static boolean isChinese(String input){
return matches(chinese, input);
}
/** ascii码*/
public static boolean isAscii(String input){
return matches(ascii, input);
}
/** 邮编*/
public static boolean isZipcode(String input){
return matches(zipcode, input);
}
public static boolean isIP4(String input){
return matches(ip4, input);
}
/** 非空*/
public static boolean isNotEmpty(String input){
return matches(notempty, input);
}
/** 图片*/
public static boolean isPicture(String input){
return matches(picture, input);
}
/** 压缩文件*/
public static boolean isRar(String input){
return matches(rar, input);
}
/** 日期*/
public static boolean isDate(String input){
return matches(date, input);
}
/** qq*/
public static boolean isQQ(String input){
return matches(qq, input);
}
public static boolean isTel(String input){
return matches(tel, input);
}
/** 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串*/
public static boolean isUserName(String input){
return matches(username, input);
}
/**字母*/
public static boolean isLetter(String input){
return matches(letter, input);
}
public static boolean isLetterAndSpace(String input){
return matches(letterAndSpace, input);
}
/** 小写字母*/
public static boolean isLowLetter(String input){
return matches(letter_l, input);
}
/** 大写字母*/
public static boolean isUpperLetter(String input){
return matches(letter_u, input);
}
/** 身份证*/
public static boolean isIDCard(String input){
return matches(idcard, input);
}
public static boolean isFloat(String input){
return matches(isFloat, input);
}
public static boolean isUFloat(String input){
return matches(isUFloat, input);
}
public static boolean isInteger(String input){
return matches(isInteger, input);
}
public static boolean isUInteger(String input){
return matches(isUInteger, input);
}
public static boolean isCarVin(String carVin){
return matches(isCarVin, carVin);
}
public static boolean matches(String regex,String input){
if(StringUtils.isBlank(input)) return false;
if(input.matches(regex))return true;
return false;
}
public static void main(String[] args) {
/*System.out.println(isInteger("1"));
System.out.println(isInteger("0"));
System.out.println(isInteger("-1"));
System.out.println(isInteger("1.0"));
System.out.println("--------------------");
System.out.println(isUInteger("-1"));
System.out.println(isUInteger("0"));
System.out.println(isUInteger("10"));
System.out.println(isUInteger("1.3"));*/
System.out.println(isLetterAndSpace("tai wan"));
}
}
文章来源:中国领先IT技术网站,如有侵权,请联系作者删除。
沈阳尚观
About Me
沈阳尚观以下课程火热招生中,linux运维、Oracle数据库、 java开发、大数据开发、嵌入式开发。详情咨询QQ:2923605081
以上是关于Java专题技术:Java-最完整正则表达式验证的主要内容,如果未能解决你的问题,请参考以下文章