广州网站制作多少钱,用ps做个人网站界面,昭通做网站,低价网站建设优化公司java正则校验#xff0c;手机号#xff0c;邮箱#xff0c;日期格式#xff0c;时间格式#xff0c;数字金额两位小数
3.58是否为金额#xff1a;true 3.582是否为金额#xff1a;false 1284789qq.com是否为email#xff1a;true 1284789qq.com是否为email#xff1…java正则校验手机号邮箱日期格式时间格式数字金额两位小数
3.58是否为金额true 3.582是否为金额false 1284789qq.com是否为emailtrue 1284789qq.com是否为emailfalse 1823753112是否为手机号false 18237531125是否为手机号true 20240127判断字符串是否为日期格式true 2024-01-27判断字符串是否为日期格式true 2024/01/27判断字符串是否为日期格式true 2024/01-27判断字符串是否为日期格式true 2024-01-27 23:23:23判断字符串是否为日期格式true 2024-01-27 23:23判断字符串是否为日期格式false 2024-1-7判断字符串是否为日期格式true 23:23:23判断字符串是否为时间格式true
部分代码源代码请移步文章附件 public class PatternUtils {/*** title 判断字符串是否是金额(小数点前后是数字即可无小数点后数据也ok小数最多2位)* param amount* return boolean [-\]?[.\d]**/private static Pattern amountPattern Pattern.compile(^(\\-|\\)?\\d(\\.\\d{0,2})?$);private static Pattern emailPattern Pattern.compile(\\w[-\\w.]*([A-Za-z0-9][-A-Za-z0-9]\\.)[A-Za-z]{2,14});private static Pattern phonePattern Pattern.compile(0?(13|14|15|18|17)[0-9]{9});public static boolean strIsAmount(String amount) {if(StringUtils.isEmpty(amount)) {return false;}return amountPattern.matcher(amount).matches();}public static boolean isEmail(String email) {if(StringUtils.isEmpty(email)) {return false;}return emailPattern.matcher(email).matches();}public static boolean isPhone(String phone) {if(StringUtils.isEmpty(phone)) {return false;}return phonePattern.matcher(phone).matches();}/*** 金额验证 - 是否为负数小数最多两位** param str* return boolean*/public static boolean isNegativeNumber(String str) {try {if (StringUtils.isNotEmpty(str)) {String source str.toString();Pattern pattern Pattern.compile(-[0-9](.[0-9]{0,2})?);if (pattern.matcher(source).matches()) {return true;} else {return false;}} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/*** 金额验证 - 是否为正数小数最多两位** param str* return boolean*/public static boolean isPositiveNumber(String str) {try {if (StringUtils.isNotEmpty(str)) {String source str.toString();Pattern pattern Pattern.compile(^[]?([0-9](.[0-9]{0,2})?)$);if (pattern.matcher(source).matches()) {return true;} else {return false;}} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/*** title 功能判断字符串是否为日期格式* param strDate* return boolean*/public static boolean strIsDateTime(String strDate) {Pattern pattern Pattern.compile(^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$);Matcher m pattern.matcher(strDate);if (m.matches()) {return true;} else {return false;}}/*** title 功能判断字符串是否为时间格式* param strTime* return boolean*/public static boolean strIsTime(String strTime) {Pattern pattern Pattern.compile(^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$);Matcher m pattern.matcher(strTime);return m.matches();}public static void main(String[] args) {String sr1 3.58;System.out.println(sr1 是否为金额strIsAmount(sr1));String sr2 3.582;System.out.println(sr2 是否为金额strIsAmount(sr2));String sr3 1284789qq.com;System.out.println(sr3 是否为emailisEmail(sr3));String sr4 1284789qq.com;System.out.println(sr4 是否为emailisEmail(sr4));String sr5 1823753112;System.out.println(sr5 是否为手机号isPhone(sr5));String sr6 18237531125;System.out.println(sr6 是否为手机号isPhone(sr6));String sr7 20240127;System.out.println(sr7 判断字符串是否为日期格式strIsDateTime(sr7));String sr71 2024-01-27;System.out.println(sr71 判断字符串是否为日期格式strIsDateTime(sr71));String sr72 2024/01/27;System.out.println(sr72 判断字符串是否为日期格式strIsDateTime(sr72));String sr73 2024/01-27;System.out.println(sr73 判断字符串是否为日期格式strIsDateTime(sr73));String sr74 2024-01-27 23:23:23;System.out.println(sr74 判断字符串是否为日期格式strIsDateTime(sr74));String sr75 2024-01-27 23:23;System.out.println(sr75 判断字符串是否为日期格式strIsDateTime(sr75));String sr76 2024-1-7;System.out.println(sr76 判断字符串是否为日期格式strIsDateTime(sr76));String sr8 23:23:23;System.out.println(sr8 判断字符串是否为时间格式strIsTime(sr8));}
}