When determining whether a string in Java as plain English, a combination of pure numbers, and numbers, usually using regular str.matches match, tell whether this string and regular expression matching the given regular.
- unicode character encoding various ranges:
- Characters: [0x4e00,0x9fa5] (or decimal [19968,40869])
- Figures: [0x30,0x39] (decimal or [48, 57])
- Lowercase letters: [0x61,0x7a] (or decimal [97, 122])
- Capital letters: [0x41,0x5a] (decimal or [65, 90])
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrValidate {
// pure digital
Private static String DIGIT_REGEX = "[0-9] +" ;
// contains digital
Private static String CONTAIN_DIGIT_REGEX = "* [0-9] *.." ;
// pure letters
Private static String LETTER_REGEX = "[ the Z-zA-A] + " ;
// contain letters
Private static String CONTAIN_LETTER_REGEX =" * [A-zA-Z] *.. " ;
// pure Chinese
Private static String CHINESE_REGEX =" [\ u4e00- \ u9fa5] " ;
// contains only letters and numbers
Private static String LETTER_DIGIT_REGEX = "^ [the Z-A-z0-9A] + $";
private static String CHINESE_LETTER_REGEX = "([\u4e00-\u9fa5]+|[a-zA-Z]+)";
private static String CHINESE_LETTER_DIGIT_REGEX = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
/**
* Determining whether the string contains only numbers and letters
*
* @Param Cycle
* @return
*/
public static boolean isLetterDigit(String str) {
return str.matches(LETTER_DIGIT_REGEX);
}
/**
* Whether the Chinese characters, not including punctuation
*
* @Param with
* @Return to true kanji
* /
public static Boolean isChinese (String CON) {
Pattern pattern = Pattern.compile(CHINESE_REGEX);
for (int i = 0; i < con.length(); i = i + 1) {
if (!pattern.matcher(
String.valueOf(con.charAt(i))).find()) {
return false;
}
}
return true;
}
/**
* Regular expression character string is determined whether
* Contain only letters, numbers and characters
*
* @Param Cycle
* @return
*/
public static boolean isLetterDigitOrChinese(String str) {
return str.matches(CHINESE_LETTER_DIGIT_REGEX);
}
/**
* Name in Chinese characters and can include letters, no other characters
*
* @param passengerName
* @return
*/
public static boolean checkChineseLetter(String passengerName) {
Pattern pattern = Pattern.compile(CHINESE_LETTER_REGEX);
Matcher Matcher = Pattern.matcher (passengerName);
IF (matcher.matches ()) {
// does not contain special characters
return to true ;
} The else {
// contains special characters
return to false ;
}
}
/**
* Determines whether a string contains punctuation (Chinese or English punctuation mark), true comprising. <br/>
* Principle: the original strings do a wash, clean all punctuation. <br/>
* At this time, if the reference ret contain punctuation, the strings of different length before and after washing, returns true; otherwise, equal in length, returns false. <br/>
*
* @Param rate
* @Return to true comprising English punctuation
* /
public static Boolean checkPunctuation (String RET) {
Boolean B = to false ;
Tmp String = RET;
// replaceAll inside regular match can empty string in English punctuation, leaving only digital, English and Chinese.
tmp.replaceAll = tmp ( "\\ P {P}", "" );
IF (! ret.length () = tmp.length ()) {
b = true;
}
return b;
}
public static boolean isDigit(String ret) {
return ret.matches(DIGIT_REGEX);
}
public static boolean isLetter(String ret) {
return ret.matches(LETTER_REGEX);
}
public static boolean hasDigit(String ret) {
return ret.matches(CONTAIN_DIGIT_REGEX);
}
public static boolean hasLetter(String ret) {
return ret.matches(CONTAIN_LETTER_REGEX);
}
public static void main(String[] args) {
// System.out.println(isLetterDigitOrChinese("33dd33") + " ------- 麦迪娜·买买提 ---------");
// System.out.println(isChinese("麦迪娜·买买提"));
// System.out.println(isChinese("这个X") + " checkChineseLetter ");
// System.out.println(isChinese("checkChineseLetter"));
// System.out.println(isChinese("checkChineseLetter3"));
System.out.println(hasDigit("99999"));
System.out.println(hasDigit("9999舅舅9"));
}
}
Reference