package com.gh.common.util; import android.util.Patterns; import java.util.regex.Matcher; import kotlin.text.Regex; /** * @author CsHeng * @Date 17/05/2017 * @Time 2:35 PM */ public class PatternUtils { // public static final Pattern VALID_EMAIL_ADDRESS_REGEX = // Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE); // public static final Pattern VALIDATE_URL_REGEX = // Pattern.compile("((http://|ftp://|https://|www.))(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"); public static boolean isEmailAddress(String emailStr) { Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(emailStr); return matcher.matches(); } public static boolean isUrlAddress(String url) { Matcher matcher = Patterns.WEB_URL.matcher(url); return matcher.matches(); } public static boolean isPhoneNum(String phone) { Matcher matcher = Patterns.PHONE.matcher(phone); return matcher.matches(); } /** * 判断字符串中是否有连续2个以上的空格 忽略 \t \r \n */ public static boolean isHasSpace(String text) { String pattern = "[\\s|\\t|\\r|\\n]{2,}"; Regex regex = new Regex(pattern); return regex.find(text, 0) != null; } /** * 替换字符串中连续2个以上的空格为一个空格 忽略 \t \r \n */ public static String replaceSpace(String text) { String pattern = "[\\s|\\t|\\r|\\n]{2,}"; String newText = text; if (isHasSpace(text)) { newText = text.replaceAll(pattern, " "); } return newText; } }