Java Generic Regular expressions

Source: Internet
Author: User

A common and common Java regular matching tool to check the legality of mailbox name, phone number, user password, postal code, etc.

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

public class Regexutils {

/**
* Verify Email
* @param email address, format: [email protected],[email protected],xxx Representative email service provider
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkemail (String email) {
String regex = "\\[email protected]\\w+\\. [A-z]+ (\\.[ a-z]+)? ";
Return pattern.matches (regex, email);
}

/**
* Verify ID Number
* @param Idcard resident ID number 15 or 18 digits, the last one may be a number or a letter
* @return Verify Success returns TRUE, validation failure returns false
*/

public static Boolean Checkidcard (String idcard)
{
String regex = "[1-9]\\d{13,16}[a-za-z0-9]{1}";
Return pattern.matches (Regex,idcard);
}

/**
* Verify mobile phone number (support international format, +86135xxxx ... (Mainland China), +00852137xxxx ... (Hong Kong, China))
* @param Mobile, Unicom, telecom operator's number segment
* Mobile Number segment: 134 (0-8), 135, 136, 137, 138, 139, 147 (expected for TD card)
*, 150, 151, 152, 157 (TD-only), 158, 159, 187 (not enabled), 188 (TD-only)
* China Unicom: 130, 131, 132, 155, 156 (world-specific), 185 (not enabled), 186 (3g)
* Telecom Number segment: 133, 153, 180 (not enabled), 189
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean Checkmobile (String mobile) {
String regex = "(\\+\\d+)? 1[3458]\\d{9}$";
Return pattern.matches (Regex,mobile);
}

/**
* Verify fixed phone number
* @param phone number, format: Country (region) phone code + area code (city codes) + phone number, such as: +8602085588447
* Country code: The standard Country (region) code for the country (region) that identifies the phone number. It contains one or more digits from 0 to 9,
* Numbers are followed by a space-delimited country (region) code.
* Area codes (city code): This may contain one or more numbers from 0 to 9, where the area or city code is placed in parentheses-
* Omit this component for countries that do not use a region or city code.
* Phone Number: This contains one or more numbers from 0 to 9
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean Checkphone (String phone) {
String regex = "(\\+\\d+)?" (\\d{3,4}\\-?)? \\d{7,8}$ ";
Return pattern.matches (regex, phone);
}

/**
* Verify integers (positive and negative integers)
* @param digit An integer between one or more digits 0-9
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkdigit (String digit) {
String regex = "\\-?" [1-9]\\d+];
Return pattern.matches (Regex,digit);
}

/**
* Verify integers and floating-point numbers (plus and minus integers and positive and negative floating-point numbers)
* @param decimals one or more 0-9 floating-point numbers, such as: 1.23,233.30
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkdecimals (String decimals) {
String regex = "\\-?" [1-9]\\d+ (\\.\\d+)? ";
Return pattern.matches (regex,decimals);
}

/**
* Verify whitespace characters
* @param blankspace whitespace characters, including: space, \ t, \ n, \ r, \f, \x0b
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkblankspace (String blankspace) {
String regex = "\\s+";
Return pattern.matches (Regex,blankspace);
}

/**
* Verify Chinese
* @param Chinese Chinese characters
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean Checkchinese (String Chinese) {
String regex = "^[\u4e00-\u9fa5]+$";
Return pattern.matches (Regex,chinese);
}

/**
* Verification Date (Month day)
* @param birthday date, format: 1992-09-03, or 1992.09.03
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkbirthday (String birthday) {
String regex = "[1-9]{4} ([-./]) \\d{1,2}\\1\\d{1,2}";
Return pattern.matches (Regex,birthday);
}

/**
* Verify URL address
* @param URL format: http://blog.csdn.net:80/xyang81/article/details/7705960? or http://www.csdn.net:80
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkurl (String URL) {
String regex = "(https?:/ /(w{3}\\.)?)? \\w+\\.\\w+ (\\.[ a-za-z]+) * (: \\d{1,5})? (/\\w*) * (\ \?? (.+=.*)? (&.+=.*)?)? ";
Return pattern.matches (regex, url);
}

/**
* * Get the URL of the first-level domain name
* http://detail.tmall.com/item.htm?spm=a230r.1.10.44.1xpDSH&id=15453106243&_u=f4ve1uq1092->> Tmall.com
*
*
* @param URL
* @return
*/
public static string GetDomain (string url) {
Pattern p = pattern.compile ("(? <=http://|\\.) [^.] *?\\. (COM|CN|NET|ORG|BIZ|INFO|CC|TV) ", pattern.case_insensitive);
Get the full domain name
Pattern p=pattern.compile ("[^//]*?\\. ( COM|CN|NET|ORG|BIZ|INFO|CC|TV) ", pattern.case_insensitive);
Matcher Matcher = p.matcher (URL);
Matcher.find ();
return Matcher.group ();
}
/**
* Matching China postcode
* @param postcode post code
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean Checkpostcode (String postcode) {
String regex = "[1-9]\\d{5}";
Return Pattern.matches (regex, postcode);
}

/**
* Match IP address (simple match, format, such as: 192.168.1.1,127.0.0.1, no matching IP segment size)
* @param ipAddress IPV4 Standard Address
* @return Verify Success returns TRUE, validation failure returns false
*/
public static Boolean checkipaddress (String ipAddress) {
String regex = "[1-9] (\\d{1,2})? \ \. (0| ([1-9] (\\d{1,2})) \\. (0| ([1-9] (\\d{1,2})) \\. (0| ([1-9] (\\d{1,2})) ";
Return pattern.matches (regex, ipAddress);
}

is included. No.
public static Boolean Checkcontainsdot (String username) {
Return Username.contains (".");
}

Whether to include a conjunction character
public static Boolean Checkcontainshyphen (String username) {
Return Username.contains ("-");
}

Password Length 6-20
public static Boolean checkuserpasswordlength (String pwd) {
return Pwd.length () > 5 && pwd.length () <21;
}

public static Boolean IsValidUserName (String un)
{
String regex = "([a-z0-9a-z-]|[ \\U4E00-\\U9FA5]) + ";
Return pattern.matches (regex, un);
}
}

Java Generic Regular expressions

Related Article

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.