java正規表現を使用するツールクラス
本論文の例では、Java正規表現ツールの具体的なコードを共有します。参考にしてください。具体的な内容は以下の通りです。
import com.google.common.base.Strings;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* Created by tookbra on 2016/4/7.
*/
public class RegexUtils {
/**
* IP
*
* @param ip
* @return boolean true, ,false,
*/
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);
}
/**
*
*
* @param email
* @return boolean true, ,false,
*/
public static boolean isEmail(String email) {
if (Strings.isNullOrEmpty(email))
return false;
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
return email.matches(regex);
}
/**
* , ,
* @param text
* @return boolean true, ,false,
*/
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();
}
/**
*
*
* @param number
*
* @return boolean true, ,false,
*/
public static boolean isNumber(String number) {
if (Strings.isNullOrEmpty(number))
return false;
String regex = "[0-9]*";
return number.matches(regex);
}
/**
* ( )
*
* @param decimal
*
* @param count
*
* @return boolean true, ,false,
*/
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);
}
/**
*
*
* @param phoneNumber
*
* @return boolean true, ,false,
*/
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);
}
/**
*
*
* @param phoneNumber
*
* @return boolean true, ,false,
*/
public static boolean isPhoneNumber(String phoneNumber) {
if (Strings.isNullOrEmpty(phoneNumber))
return false;
String regex = "^1\\d{10}$";
return phoneNumber.matches(regex);
}
/**
*
*
* @param text
* @return boolean true, ,false,
*/
public static boolean hasSpecialChar(String text) {
if (Strings.isNullOrEmpty(text))
return false;
if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
//
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;
}
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。