This article has shared the specific code of Java regular expression tool for your reference. The specific content is as follows
import com.google.common.base.Strings;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Commonly used regular expressions* Created by tookbra on 2016/4/7. */public class RegexUtils { /** * Determine whether it is the correct IP address* * @param ip * @return boolean true,pass, false, failed */ public static boolean isIp(String ip) { if (Strings.isNullOrEmpty(ip)) return false; String regex = "^(1//d{2}|2[0-4]//d|25[0-5]|[1-9]//d|[1-9])//." + "(1//d{2}|2[0-4]//d|25[0-5]|[1-9]//d|//d)//." + "(1//d{2}|2[0-4]//d|25[0-5]|[1-9]//d|/d)//." + "(1//d{2}|2[0-4]//d|25[0-5]|[1-9]//d|//d)$"; return ip.matches(regex); } /** * Determine whether it is the correct email address* * @param email * @return boolean true, passed, false, failed */ public static boolean isEmail(String email) { if (Strings.isNullOrEmpty(email)) return false; String regex = "//w+([-+.]//w+)*@//w+([-.]//w+)*//w+([-.]//w+)*"; return email.matches(regex); } /** * Determine whether it contains Chinese, only suitable for Chinese characters, not including punctuation* @param text * @return boolean true, passed, false, failed */ public static boolean isChinese(String text) { if (Strings.isNullOrEmpty(text)) return false; Pattern p = Pattern.compile("[/u4e00-/u9fa5]"); Matcher m = p.matcher(text); return m.find(); } /** * Determine whether a positive integer* * @param number * Number* @return boolean true, pass, false, failed */ public static boolean isNumber(String number) { if (Strings.isNullOrEmpty(number)) return false; String regex = "[0-9]*"; return number.matches(regex); } /** * Determine how many decimals (positive numbers) * * @param decimal * Number* @param count * Decimal digits* @return boolean true, pass, false, failed */ public static boolean isDecimal(String decimal, int count) { if (Strings.isNullOrEmpty(decimal)) return false; String regex = "^(-)?(([1-9]{1}//d*)|([0]{1}))(//.(//d){" + count + "})?$"; return decimal.matches(regex); } /** * Determine whether it is a mobile phone number* * @param phoneNumber * Mobile phone number* @return boolean true, pass, false, failed */ public static boolean isMobilePhoneNumber(String phoneNumber) { if (Strings.isNullOrEmpty(phoneNumber)) return false; String regex = "^((13[0-9])|(15[0-9])|(18[1-9]))//d{8}$"; return phoneNumber.matches(regex); } /** * Determine whether it is a mobile phone number* * @param phoneNumber * Mobile phone number* @return boolean true, pass, false, failed */ public static boolean isPhoneNumber(String phoneNumber) { if (Strings.isNullOrEmpty(phoneNumber)) return false; String regex = "^1//d{10}$"; return phoneNumber.matches(regex); } /** * Determine whether it contains special characters* * @param text * @return boolean true, pass, false, failed */ public static boolean hasSpecialChar(String text) { if (Strings.isNullOrEmpty(text)) return false; if (text.replaceAll("[az]*[AZ]*//d*-*_*//s*", "").length() == 0) { // If special characters are not included return true; } return false; } private static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.