这是经常使用到的一些基础推断,记录下来:
包含是:推断整数、推断邮箱、推断手机号码、推断邮政编码、推断中文
import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; public class StringHelper { private StringHelper() { } /** * 推断str是否整数 * * @param str * @return */ public static boolean isInteger(String str) { return NumberUtils.isDigits(str); } /** * 推断str是否数字(整数、小数、科学计数法等等格式) * * @param str * 非空字符串 * @return */ public static boolean isNumber(String str) { return NumberUtils.isNumber(str); } private static final String REG_CHINESE = "^[\u4e00-\u9fa5]+$"; private static final String REG_EMAIL = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; protected static boolean isMatch(String str, String regPattern) { if (str == null || str.length() == 0) return false; Pattern p = Pattern.compile(regPattern); Matcher m = p.matcher(str); return m.matches(); } /** * 是否是合格的手机号码格式 * * @param phoneNum * 非空字符串电话号码 * @return true 合法的手机号 */ public static boolean isPhoneNum(String phoneNum) { return isMatch(phoneNum, "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); } // /** * 推断email格式是否正确 * * @param email * 非空字符串 * @return */ public static boolean isEmail(String email) { return isMatch(email, REG_EMAIL); } /** * 匹配中国邮政编码 * * @param str * 非空字符串 * @return */ public static boolean isPostCode(String str) { return isMatch(str, "^[1-9]\\d{5}$"); } /** * 中国的身份证为15位或18位 * * @param str * 非空字符串 * @return */ public static boolean isIdCard(String str) { return isMatch(str, "(^\\d{17}([0-9]|X)|^\\d{15}$"); } /** * 剔除首尾空格 * * @param str * @return 剔除首尾空格 */ public static String trim(String str) { return StringUtils.trim(str); } /** * 非空字符串是否是中文 * * @param str * 非空字符串 * @return 全是中文字符为true */ public static boolean isChinese(String str) { if (str == null) { return false; } if (str.length() == 0) { return false; } Pattern pt = Pattern.compile(REG_CHINESE); Matcher mt = pt.matcher(str); return mt.matches(); } }