Common java Regular Expression tools and Regular Expressions

Source: Internet
Author: User

Common java Regular Expression tools and Regular Expressions

The examples in this article share the code of the java Regular Expression tool class for your reference. The specific content is as follows:

Import com. google. common. base. strings; import java. util. regex. matcher; import java. util. regex. pattern;/*** common Regular Expression * Created by tookbra on 2016/4/7. */public class RegexUtils {/*** determine whether the ip address is correct ** @ param ip * @ return boolean true, pass, false, */public static boolean isIp (String ip) {if (Strings. isNullOrEmpty (ip) return false; string regex = "^ (1 \ d {2} | 2 [0-4] \ d | 25 [0-5] | [1-9] \ d | [1-9]) \\. "+" (1 \ d {2} | 2 [0-4] \ d | 25 [0-5] | [1-9] \ d | \ d) \\. "+" (1 \ d {2} | 2 [0-4] \ d | 25 [0-5] | [1-9] \ d | \\ d) \\. "+" (1 \ d {2} | 2 [0-4] \ d | 25 [0-5] | [1-9] \ d | \\ d) $ "; return ip. matches (regex);}/*** determine whether the email address is correct ** @ param email * @ return boolean true, pass, false, failed to pass */public static boolean isEmail (String email) {if (Strings. isNullOrEmpty (email) return false; String regex = "\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\\. \ w + ([-.] \ w +) * "; Return email. matches (regex);}/*** determines whether it contains Chinese characters. It is only applicable to Chinese characters and does not contain punctuation marks * @ param text * @ return boolean true, pass, false, */public static boolean isChinese (String text) {if (Strings. isNullOrEmpty (text) return false; Pattern p = Pattern. compile ("[\ u4e00-\ u9fa5]"); Matcher m = p. matcher (text); return m. find ();}/*** determine whether a positive integer ** @ param number * @ return boolean true, pass, false, not pass */public static B Oolean isNumber (String number) {if (Strings. isNullOrEmpty (number) return false; String regex = "[0-9] *"; return number. matches (regex);}/*** determines the number of decimal places (positive) ** @ param decimal * Number * @ param count * decimal places * @ return boolean true, false, not passed */public static boolean isDecimal (String decimal, int count) {if (Strings. isNullOrEmpty (decimal) return false; String regex = "^ (-)? ([1-9] {1} \ d *) | ([0] {1 }))(\\. (\ d) {"+ count + "})? $ "; Return decimal. matches (regex);}/*** determine whether it is a mobile phone number ** @ param phoneNumber * Mobile Phone Number * @ return boolean true, pass, false, */public static boolean isMobilePhoneNumber (String phoneNumber) {if (Strings. isNullOrEmpty (phoneNumber) return false; String regex = "^ (13 [0-9]) | (15 [0-9]) | (18 [1-9]) \ d {8} $ "; return phoneNumber. matches (regex);}/*** determine whether it is a mobile phone number ** @ param phoneNumber * Mobile Phone Number * @ return boolean true, pass, false, */public static boolean isPhoneNumber (String phoneNumber) {if (Strings. isNullOrEmpty (phoneNumber) return false; String regex = "^ 1 \ d {10} $"; return phoneNumber. matches (regex);}/*** determines whether a special character is contained ** @ param text * @ return boolean true, pass, false, failed to pass */public static boolean hasSpecialChar (String text) {if (Strings. isNullOrEmpty (text) return false; if (text. replaceAll ("[a-z] * [A-Z] * \ d *-* _ * \ s *",""). length () = 0) {// if it does not contain special characters return true;} return false;} private static boolean isChinese (char c) {Character. unicodeBlock ub = Character. unicodeBlock. of (c); if (ub = Character. unicodeBlock. cjk_uniied_ideographs | ub = Character. unicodeBlock. CJK_COMPATIBILITY_IDEOGRAPHS | ub = Character. unicodeBlock. CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | ub = Character. unicodeBlock. CJK_UNIFIED_IDEOGRAPHS_EXTENSION_ B | ub = Character. unicodeBlock. CJK_SYMBOLS_AND_PUNCTUATION | ub = Character. unicodeBlock. HALFWIDTH_AND_FULLWIDTH_FORMS | ub = Character. unicodeBlock. GENERAL_PUNCTUATION) {return true;} return false ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.