Generally in the Write user Registration module when the registered user name is judged, see if there is [email protected]#$%^&* () and other such special characters, the registration information is generally considered illegal characters are not allowed to register. How to judge, the use of the regular expression here.
Let's start by explaining the following regular expressions:
String Chinese = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d]+$"; /** This regular expression is used to determine whether it is Chinese **/
Used for user registration. Matches a string consisting of a number, 26 letters, or underscores
String username = "^\\w+$"; /** This regular expression to determine whether the word character is: [a-za-z_0-9]**/
String all = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d\\w]{2,10}$"; /** This regular expression combines the two above to determine the Chinese, uppercase and lowercase letters and numbers, and the {2,10} character length is 2-10**/
The following is a complete example of a program that has been tested and run through:
[Java]View Plaincopy
- Import Java.util.regex.Pattern;
- Public class Replacealltest {
- public static void Main (string[] args) {
- String Chinese = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d]+{1}quot;; /** This regular expression is used to determine whether it is Chinese **/
- String username = "^\\w+{1}quot;; /** This regular expression to determine whether the word character is: [a-za-z_0-9]**/
- /** This regular expression combines the above two to judge, Chinese, uppercase and lowercase letters and numbers **/
- String all = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d\\w]{2,10}{1}quot;; //{2,10} indicates that the length of the character is 2-10
- Pattern pattern = pattern.compile (all);
- Boolean tf = Pattern.matcher ("12345"). Matches ();
- Boolean tf = Pattern.matches (All, "Liu Yan L12 Hall to right See");
- SYSTEM.OUT.PRINTLN (TF);
- \\u4E00-\\u9FA5\\uF900-\\uFA2D refers to the Unicode encoding range of Chinese characters
- \w refers to the word character: [a-za-z_0-9]
- }
- }
Regular expressions to determine whether the user registration information is Chinese characters, letters and numbers