Java tutorial
/** * Copyright (c) 2015, biezhi (biezhi.me@gmail.com) * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wyb.utils.util; import org.apache.commons.lang3.StringUtils; import java.util.regex.Pattern; /** * * ?????????????? * @author Kunzite */ public final class PatternUtil { /** * ?Email * * @param email email??zhangsan@sina.comzhangsan@xxx.com.cnxxx? * @return ??true?false */ public static boolean isEmail(String email) { if (StringUtils.isBlank(email)) { return false; } String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; return Pattern.matches(regex, email); } /** * ???? * * @param idCard ???15?18????? * @return ??true?false */ public static boolean isIdCard(String idCard) { if (StringUtils.isBlank(idCard)) { return false; } String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}"; return Pattern.matches(regex, idCard); } /** * ?????+86135xxxx...+00852137xxxx... * * @param mobile ??????? * <p>?134(0-8)?135?136?137?138?139?147TD? * ?150?151?152?157TD?158?159?187??188TD</p> * <p>??130?131?132?155?156?185??1863g</p> * <p>?133?153?180??189</p> * @return ??true?false */ public static boolean isMobile(String mobile) { if (StringUtils.isBlank(mobile)) { return false; } String regex = "(\\+\\d+)?1[34578]\\d{9}$"; return Pattern.matches(regex, mobile); } /** * ???? * * @param tellPhone ?????? + ?? + ???+8602085588447 * <p><b> ? </b>????? 0 9 ?? * ??</p> * <p><b>??</b>?? 0 9 ?? * ???</p> * <p><b>???</b>? 0 9 </p> * @return ??true?false */ public static boolean isTellPhone(String tellPhone) { if (StringUtils.isBlank(tellPhone)) { return false; } String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; return Pattern.matches(regex, tellPhone); } /** * ? * * @param digit ??0-9 * @return ??true?false */ public static boolean isDigit(String digit) { if (StringUtils.isBlank(digit)) { return false; } String regex = "\\-?[1-9]\\d+"; return Pattern.matches(regex, digit); } /** * ? * * @param decimals ??0-91.23233.30 * @return ??true?false */ public static boolean isDecimals(String decimals) { if (StringUtils.isBlank(decimals)) { return false; } String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; return Pattern.matches(regex, decimals); } /** * ? * * @param chinese * @return ??true?false */ public static boolean isChinese(String chinese) { if (StringUtils.isNotBlank(chinese)) { String regex = "^[\u4E00-\u9FA5]+$"; return Pattern.matches(regex, chinese); } return false; } /** * ? * * @param str * @return */ public static boolean isNumber(String str) { if (StringUtils.isNotBlank(str)) { String regex = "^[1-9]\\d*$"; return Pattern.matches(regex, str); } return false; } /** * ? * * @param birthday ?1992-09-031992.09.03 * @return ??true?false */ public static boolean isBirthday(String birthday) { if (StringUtils.isNotBlank(birthday)) { String regex = "^(\\d{4})-(\\d{2})-(\\d{2})$"; return Pattern.matches(regex, birthday); } return false; } /** * ?URL? * * @param url ?http://blog.csdn.net:80/xyang81/article/details/7705960? http://www.csdn.net:80 * @return ??true?false */ public static boolean isURL(String url) { if (StringUtils.isNotBlank(url)) { String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?"; return Pattern.matches(regex, url); } return false; } /** * ?? * * @param postCode ? * @return ??true?false */ public static boolean isPostcode(String postCode) { if (StringUtils.isNotBlank(postCode)) { String regex = "[1-9]\\d{5}"; return Pattern.matches(regex, postCode); } return false; } /** * ?IP?(???192.168.1.1127.0.0.1?IP?) * * @param ipAddress IPv4? * @return ??true?false */ public static boolean isIpAddress(String ipAddress) { if (StringUtils.isNotBlank(ipAddress)) { String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))"; return Pattern.matches(regex, ipAddress); } return false; } /** * ??? 616??? ? * * @param sourceVal * @return */ public static boolean isUserName(String sourceVal) { if (StringUtils.isBlank(sourceVal)) { return false; } String regex = "^[a-zA-Z][0-9A-Za-z]{5,15}$"; boolean isOk = sourceVal.matches(regex); return isOk; } /** * ? * @param sourceVal * @param max * @return */ public static boolean MaxLength(String sourceVal, int max) { if (StringUtils.isBlank(sourceVal)) { return false; } // String regex = "[\\S]{0,"+max+"}"; // String regex = "[\\S][\\W]{0,"+max+"}"; String regex = "[\\s\\S]{0," + max + "}"; boolean isOk = sourceVal.matches(regex); return isOk; } /** * 6??? * @param sourceVal * @return */ public static boolean yzCode(String sourceVal) { if (StringUtils.isBlank(sourceVal)) { return false; } String regex = "^[0-9]{6}"; return sourceVal.matches(regex); } /** * ? 6-16 ?? * @param sourceVal * @return */ public static boolean password(String sourceVal) { if (StringUtils.isBlank(sourceVal)) { return false; } String regex = "^[0-9A-Za-z]{6,16}$"; return sourceVal.matches(regex); } /** * */ public static boolean charsetDeal(String sourceVal) { if (StringUtils.isBlank(sourceVal)) { return false; } String regex = "^[\\u4e00-\\u9fa5a-zA-Z0-9]+$"; return sourceVal.matches(regex); } }