Package com.sangedabuliu.www; Import Java.util.regex.matcher;import Java.util.regex.Pattern; public class Userreg {/** * validates username, supports both Chinese and English (including full-width characters), numbers, underscores, and minus signs (full-width and kanji two-bit), 4-20-bit length, Chinese by two-bit count * @author Www.sangedabul iu.com * @param userName * @return * * public static Boolean validateusername (String userName) {Stri ng validatestr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uff21-\uff3a\uff41-\uff5a]+$"; Boolean rs = false; rs = Matcher (Validatestr, userName); if (RS) {int strlenth = getstrlength (userName); if (Strlenth < 4 | | Strlenth >) {rs = false; }} return RS; }/** * Gets the length of the string, the double character (including kanji) is counted by two bits * * @param value * @return */public static int Getstrlength ( String value) {int valuelength = 0; String Chinese = "[\u0391-\uffe5]"; for (int i = 0; i < value.length (); i++) {String temp = value.substring (i, i + 1); if (Temp.matches (Chinese)) {valuelength + = 2; } else {valuelength + = 1; }} return valuelength; } private static Boolean Matcher (String Reg, String string) {Boolean tem = FALSE; Pattern pattern = Pattern.compile (reg); Matcher Matcher = Pattern.matcher (string); TEM = matcher.matches (); return tem; } public static void Main (string[] args) {String str = "0-_f9zd 22"; String st = "a-dq_!!!! Go to symbol Labels! ノチセたのひちぬ! Of course.!!.. * * Half angle "; SYSTEM.OUT.PRINTLN (Validateusername (str)); System.out.println (St.replaceall ("[\\pp&&[^-_]]", "")); System.out.println (St.replaceall ("[\\w\\--Calls A-za-z]", "")); }}
Java Code Validation user name, support Chinese and English (including full-width characters), numbers, underscores and minus signs (full-width and kanji two-bit), 4-20-bit length, two-bit count