Regular expressions to determine whether the user registration information is Chinese characters, letters and numbers

Source: Internet
Author: User

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
    1. Import Java.util.regex.Pattern;
    2. Public class Replacealltest {
    3. public static void Main (string[] args) {
    4. String Chinese = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d]+{1}quot;; /** This regular expression is used to determine whether it is Chinese **/
    5. String username = "^\\w+{1}quot;; /** This regular expression to determine whether the word character is: [a-za-z_0-9]**/
    6. /** This regular expression combines the above two to judge, Chinese, uppercase and lowercase letters and numbers **/
    7. String all = "^[\\u4e00-\\u9fa5\\uf900-\\ufa2d\\w]{2,10}{1}quot;; //{2,10} indicates that the length of the character is 2-10
    8. Pattern pattern = pattern.compile (all);
    9. Boolean tf = Pattern.matcher ("12345"). Matches ();
    10. Boolean tf = Pattern.matches (All, "Liu Yan L12 Hall to right See");
    11. SYSTEM.OUT.PRINTLN (TF);
    12. \\u4E00-\\u9FA5\\uF900-\\uFA2D refers to the Unicode encoding range of Chinese characters
    13. \w refers to the word character: [a-za-z_0-9]
    14. }
    15. }

Regular expressions to determine whether the user registration information is Chinese characters, letters and numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.