public class UtilRegular {
//整数
public static final String intege = "^-?[1-9]\\d*$";
//正整数
public static final String justIntege = "^[1-9]\\d*$";
//负整数
public static final String negtiveIntege = "^-[1-9]\\d*$";
//数字
public static final String number = "^([+-]?)\\d*\\.?\\d+$";
//整数+0
public static final String intege0 = "^[1-9]\\d*|0$";
//负整数+0
public static final String negtiveIntege0 = "^-[1-9]\\d*|0$";
//浮点数
public static final String floatPoint = "^([+-]?)\\d\\.\\d+$";
//正浮点数
public static final String justFloatPoint = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
//负浮点数 "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$"
public static final String negtiveFloatPoint = "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$";
//非负浮点数
public static final String notNegtiveFloatPoint = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$";
//非正浮点数
public static final String notJustFloatPoint = "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$";
//邮箱地址
public static final String email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
//颜色值匹配
public static final String colorNumber = "^[a-fA-F0-9]{6}$";
//url 匹配
public static final String URL ="^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$";
//纯仅中文字符
public static final String chinese = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
//仅ACSII字符
public static final String acsii = "^[\\x00-\\xFF]+$";
//邮政编码
public static final String zipCode = "^\\d{6}$";
//国内手机号码
public static final String modile = "^(13|15|16|17|18)[0-9]{9}$";
//IP V4地址
public static final String ipv4 = "^(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)$";
//非null字符
public static final String notNull = "^\\S+$";
//图片后缀
public static final String image = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga|JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA)$";
//音频后缀
public static final String audio = "(.*)\\.(mp3|wma|mid|midi|wav|vqf|MP3|WMA|MID|MIDI|WAV|VQF)$";
//视频后缀
public static final String video = "(.*)\\.(rm|3gp|mp4|rmvb|avi|wmv|flv|vob|exe|mkv|swf|RM|3GP|MP4|RMVB|AVI|WMV|FLV|VOB|EXE|MKV|SWF)$";
//压缩文件后缀
public static final String rar = "(.*)\\.(rar|zip|7zip|tgz|RAR|ZIP|7ZIP|TGZ)$";
//日期格式
public static final String date = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$";
//日期和时间格式
public static final String dateTime = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}(\\s\\d{2}:)?(\\d{2}:)?(\\d{2})?$";
//QQ号码
public static final String qq = "^[1-9]*[1-9][0-9]*+\\@+(qq|QQ)+\\.+com$";
//电话号码的函数
public static final String tel = "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$";
//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
public static final String username = "^[A-Za-z]\\w{5,}$";
//字母数字组合
public static final String allstring = "^\\w+$";
//纯字母
public static final String onlyString = "^[A-Za-z]+$";
//纯大写字母
public static final String onlyCapsString = "^[A-Z]+$";
//纯小写字母
public static final String onlySmallString = "^[a-z]+$";
//第二代身份证号码匹配
public static final String idCard = "^[1-9]([0-9]{14}|[0-9]{17})$";
//数字或字母
public static final String numberOrString = "^[A-Za-z0-9]+$";
public static void main(String[] arg){
//整数
// regularIntege();
//正整数
// regularJustIntege();
//负整数
// negtiveIntege();
//数字
// regularNumber();
//正数(正整数+0)
// regularIntege0();
//负数(负整数+0)
// negtiveIntege0();
//浮点数
// regularFloatPoint();
//正浮点数
// regularJustFloatPoint();
//负浮点数
// regularNegtiveFloatPoint();
//非负浮点数(正浮点数+0)
// regularJustFloatPoint0();
//非正浮点数(负浮点数+0)
// regularNegtiveFloatPoint0();
//邮箱地址
// regularEmail();
//颜色值匹配
// regularColorNumber();
//url匹配
// regularUrl();
//纯仅中文字符
// regularChinese();
//仅ACSII字符
// regularAcsii();
//邮政编码
// regularZipCode();
//国内手机号
// regularMobile();
//IP V4地址
// regularIpv4();
//非空字符
// regularNotEmpty();
//图片后缀
// regularImage();
//音频后缀
// regularAudio();
//视频后缀
// regularVideo();
//压缩文件后缀
// regularRar();
//日期格式
// regularDate();
//日期和时间格式
// regularDateTime();
//QQ号码
// regularQQ();
//电话号码的函数(包括验证国内区号,国际区号,分机号)
// regularTel();
//用户名注册
// regularUserRegister();
//字母数字组合
// regularAllString();
//纯字母
// regularOnlyString();
//纯大写字母
// regularOnlyCapsString();
//纯小写字母
// regularOnlySmallString();
//第二代身份证号码匹配
// regularIdCard();
//数字或字母
regularNumberOrString();
}
//数字或字母
private static void regularNumberOrString() {
System.out.println(Pattern.matches(numberOrString,"abc123")); //true
System.out.println(Pattern.matches(numberOrString,"abc")); //true
System.out.println(Pattern.matches(numberOrString,"123")); //true
System.out.println(Pattern.matches(numberOrString,"ab@1122")); //true
}
//第二代身份证号码匹配
private static void regularIdCard() {
System.out.println(Pattern.matches(idCard,"123456112233445566")); //true
System.out.println(Pattern.matches(idCard,"1122334455")); //false
}
//纯小写字母
private static void regularOnlySmallString() {
System.out.println(Pattern.matches(onlySmallString,"stt")); //true
System.out.println(Pattern.matches(onlySmallString,"a")); //true;
System.out.println(Pattern.matches(onlySmallString,"Stra")); //false
}
//纯大写字母
private static void regularOnlyCapsString() {
System.out.println(Pattern.matches(onlyCapsString,"STR")); //true
System.out.println(Pattern.matches(onlyCapsString,"A")); //true
System.out.println(Pattern.matches(onlyCapsString,"str")); //false
System.out.println(Pattern.matches(onlyCapsString,"Str123")); //false
}
//纯字母
private static void regularOnlyString() {
System.out.println(Pattern.matches(onlyString,"str")); //true
System.out.println(Pattern.matches(onlyString,"A")); //true
System.out.println(Pattern.matches(onlyString,"str123")); //false
System.out.println(Pattern.matches(onlyString,"123456")); //false
}
//字母数字组合
private static void regularAllString() {
System.out.println(Pattern.matches(allstring,"test123")); //true
System.out.println(Pattern.matches(allstring,"test@#")); //false
System.out.println(Pattern.matches(allstring,"123.1")); //false
}
//用户名注册
private static void regularUserRegister() {
// 正例
System.out.println(Pattern.matches(username,"usernametest")); // true
// 反例
System.out.println(Pattern.matches(username,"username@test")); // false
}
//电话号码的函数
private static void regularTel() {
// 正例
System.out.println(Pattern.matches(tel,"010-1234567")); // true
System.out.println(Pattern.matches(tel,"0100-12345678")); // true
// 反例
System.out.println(Pattern.matches(tel,"13800138000")); // false
}
//qq号码
private static void regularQQ() {
System.out.println(Pattern.matches(qq,"2383@qq.com")); //true
System.out.println(Pattern.matches(qq,"test@huohuo.com")); //false
}
//日期和时间格式
private static void regularDateTime() {
System.out.println(Pattern.matches(dateTime,"2024-10-24 23:59:59")); //true
System.out.println(Pattern.matches(dateTime,"2024/10/24 23:59:59")); //true
System.out.println(Pattern.matches(dateTime,"2024年10月24日 23时59分59秒")); //false
}
//日期格式
private static void regularDate() {
System.out.println(Pattern.matches(date,"2024-10-24")); //true
System.out.println(Pattern.matches(date,"2024/10/24")); //true
System.out.println(Pattern.matches(date,"2024年10月24日")); //false
}
//压缩文件后缀
private static void regularRar() {
System.out.println(Pattern.matches(rar,"tt.zip")); //true
System.out.println(Pattern.matches(rar,"tt.zip1")); //false
}
//视频后缀
private static void regularVideo() {
System.out.println(Pattern.matches(video,"tt.mp4")); //true
System.out.println(Pattern.matches(video,"tt.pp")); //false
}
//音频后缀
private static void regularAudio() {
System.out.println(Pattern.matches(audio,"tt.mp3")); //true
System.out.println(Pattern.matches(audio,"tt.mp4")); //false
}
//图片后缀
private static void regularImage() {
System.out.println(Pattern.matches(image,"tt.jpg")); //true
System.out.println(Pattern.matches(image,"mm.pp")); //false
}
//非空字符
private static void regularNotEmpty() {
System.out.println(Pattern.matches(notNull,"test")); //true
System.out.println(Pattern.matches(notNull," abc ")); // false
System.out.println(Pattern.matches(notNull,"")); //false
}
//IP V4地址
private static void regularIpv4() {
// 正例
System.out.println(Pattern.matches(ipv4,"127.0.0.1")); // true
// 反例
System.out.println(Pattern.matches(ipv4,"aa.bb.cc.dd")); // false
}
//国内手机号
private static void regularMobile() {
System.out.println(Pattern.matches(modile,"18201702456")); //true
System.out.println(Pattern.matches(modile,"11204703456")); //false
}
//邮政编码
private static void regularZipCode() {
System.out.println(Pattern.matches(zipCode,"123456")); //true
System.out.println(Pattern.matches(zipCode,"12345")); //false
System.out.println(Pattern.matches(zipCode,"12345a")); //false
}
//仅ACSII字符
private static void regularAcsii() {
System.out.println(Pattern.matches(acsii,"abc123")); //true
System.out.println(Pattern.matches(acsii,"にそ①②③1")); //false
}
//仅纯中文字符
private static void regularChinese() {
System.out.println(Pattern.matches(chinese,"天地无极")); //true
System.out.println(Pattern.matches(chinese,"天地无极1")); //false
System.out.println(Pattern.matches(chinese,"天地无极2")); //false
}
//URL匹配
private static void regularUrl() {
System.out.println(Pattern.matches(URL,"https://www.2345.com/?k128910"));
System.out.println(Pattern.matches(URL,"http://www.2345.com/?k128910"));
System.out.println(Pattern.matches(URL,"www.2345.com"));
}
//颜色值匹配
private static void regularColorNumber() {
// 正例
System.out.println(Pattern.matches(colorNumber,"ffffff")); // true
System.out.println(Pattern.matches(colorNumber,"FFFFFF")); // true
System.out.println(Pattern.matches(colorNumber,"abcdef")); //true
// 反例
System.out.println(Pattern.matches(colorNumber,"#FFFFFF")); // false
System.out.println(Pattern.matches(colorNumber,"white")); // false
}
//邮箱地址
private static void regularEmail() {
System.out.println(Pattern.matches(email,"zmc@qq.com")); //true
System.out.println(Pattern.matches(email,"zmc.qq.com")); //false
}
//非正浮点数(负浮点数+0)
private static void regularNegtiveFloatPoint0() {
System.out.println(Pattern.matches(notJustFloatPoint,"-0.1")); //true
System.out.println(Pattern.matches(notJustFloatPoint,"0")); //true
System.out.println(Pattern.matches(notJustFloatPoint,"0.1")); //false
System.out.println(Pattern.matches(notJustFloatPoint,"-a.b")); //false
}
//非负浮点数(正浮点数+0)
private static void regularJustFloatPoint0() {
System.out.println(Pattern.matches(notNegtiveFloatPoint,"0.1")); // true
System.out.println(Pattern.matches(notNegtiveFloatPoint,"0")); //true
System.out.println(Pattern.matches(notNegtiveFloatPoint,"-0.1")); // false
}
//负浮点数
private static void regularNegtiveFloatPoint() {
System.out.println(Pattern.matches(negtiveFloatPoint,"-0.1")); //true
System.out.println(Pattern.matches(negtiveFloatPoint,"0.1")); //false
System.out.println(Pattern.matches(negtiveFloatPoint,"-a.b")); //false
}
//正浮点数
private static void regularJustFloatPoint() {
System.out.println(Pattern.matches(justFloatPoint,"0.11")); //true
System.out.println(Pattern.matches(justFloatPoint,"-1.11")); //false
System.out.println(Pattern.matches(justFloatPoint,"a.bc")); //false
}
//浮点数
private static void regularFloatPoint() {
System.out.println(Pattern.matches(floatPoint,"1.23")); //true
System.out.println(Pattern.matches(floatPoint,"-0.11")); //true
System.out.println(Pattern.matches(floatPoint,"123")); //false
System.out.println(Pattern.matches(floatPoint,"abc")); //fasle
}
//负数+0
private static void negtiveIntege0() {
System.out.println(Pattern.matches(negtiveIntege0,"-123")); //true
System.out.println(Pattern.matches(negtiveIntege0,"0")); //true
System.out.println(Pattern.matches(negtiveIntege0,"123")); //false
}
//正整数+0
private static void regularIntege0() {
System.out.println(Pattern.matches(intege0,"123")); //true
System.out.println(Pattern.matches(intege0,"0")); //true;
System.out.println(Pattern.matches(intege0,"abc")); //false
}
//数字
private static void regularNumber() {
System.out.println(Pattern.matches(number,"123")); //true
System.out.println(Pattern.matches(number,"-123")); //true
System.out.println(Pattern.matches(number,"0.01")); //true
System.out.println(Pattern.matches(number,"ab123")); //false
System.out.println(Pattern.matches(number,"abc")); //false
}
//负整数
private static void negtiveIntege() {
System.out.println(Pattern.matches(negtiveIntege,"123456")); //false
System.out.println(Pattern.matches(negtiveIntege,"-1230")); //true
System.out.println(Pattern.matches(negtiveIntege,"abc")); //false
}
//正整数
private static void regularJustIntege() {
System.out.println(Pattern.matches(justIntege,"123")); //true
System.out.println(Pattern.matches(justIntege,"-123")); //false
System.out.println(Pattern.matches(justIntege,"abc")); //false
}
//整数
public static void regularIntege(){
//正例
System.out.println(Pattern.matches(intege,"123")); //true
System.out.println(Pattern.matches(intege,"-123")); //true
//反例
System.out.println(Pattern.matches(intege,"abc")); //false
System.out.println(Pattern.matches(intege,"0")); //false
}
}
参考: