Android常用正则工具类

此类提供日常开发中常用的正则验证函数,比如:邮箱、手机号、电话号码、身份证号码、日期、数字、小数、URL、IP地址等。使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        return m.matches();
每个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论。下面是工具类的完整代码:

    /**
     * 正则工具类
     * 提供验证邮箱、手机号、电话号码、身份证号码、数字等方法
     */
    public final class RegexUtils {  

        /**
         * 验证Email
         * @param email email地址,格式:[email protected],[email protected],xxx代表邮件服务商
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkEmail(String email) {
            String regex = "\\[email protected]\\w+\\.[a-z]+(\\.[a-z]+)?";
            return Pattern.matches(regex, email);
        }  

        /**
         * 验证身份证号码
         * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkIdCard(String idCard) {
            String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
            return Pattern.matches(regex,idCard);
        }  

        /**
         * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
         * @param mobile 移动、联通、电信运营商的号码段
         *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
         *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>
         *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>
         *<p>电信的号段:133、153、180(未启用)、189</p>
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkMobile(String mobile) {
            String regex = "(\\+\\d+)?1[3458]\\d{9}$";
            return Pattern.matches(regex,mobile);
        }  

        /**
         * 验证固定电话号码
         * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
         * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,
         *  数字之后是空格分隔的国家(地区)代码。</p>
         * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
         * 对不使用地区或城市代码的国家(地区),则省略该组件。</p>
         * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkPhone(String phone) {
            String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
            return Pattern.matches(regex, phone);
        }  

        /**
         * 验证整数(正整数和负整数)
         * @param digit 一位或多位0-9之间的整数
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkDigit(String digit) {
            String regex = "\\-?[1-9]\\d+";
            return Pattern.matches(regex,digit);
        }  

        /**
         * 验证整数和浮点数(正负整数和正负浮点数)
         * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkDecimals(String decimals) {
            String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
            return Pattern.matches(regex,decimals);
        }   

        /**
         * 验证空白字符
         * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkBlankSpace(String blankSpace) {
            String regex = "\\s+";
            return Pattern.matches(regex,blankSpace);
        }  

        /**
         * 验证中文
         * @param chinese 中文字符
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkChinese(String chinese) {
            String regex = "^[\u4E00-\u9FA5]+$";
            return Pattern.matches(regex,chinese);
        }  

        /**
         * 验证日期(年月日)
         * @param birthday 日期,格式:1992-09-03,或1992.09.03
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkBirthday(String birthday) {
            String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
            return Pattern.matches(regex,birthday);
        }  

        /**
         * 验证URL地址
         * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
         * @return 验证成功返回true,验证失败返回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);
        }  

        /**
         * 匹配中国邮政编码
         * @param postcode 邮政编码
         * @return 验证成功返回true,验证失败返回false
         */
        public static boolean checkPostcode(String postcode) {
            String regex = "[1-9]\\d{5}";
            return Pattern.matches(regex, postcode);
        }  

        /**
         * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)
         * @param ipAddress IPv4标准地址
         * @return 验证成功返回true,验证失败返回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);
        }  

    }  
     * 正则表达式工具类测试
     */
    public class RegexUtilsTest {  

        /**
         * 验证邮箱
         */
        @Test
        public void testCheckEmail() {
            boolean result = RegexUtils.checkEmail("[email protected]");
            Assert.assertTrue(result);
        }  

        /**
         * 验证身份证号码
         */
        @Test
        public void testCheckIdCard() {
            boolean result = RegexUtils.checkIdCard("432403193902273273");
            Assert.assertTrue(result);
        }  

        /**
         * 验证手机号码
         */
        @Test
        public void testCheckMobile() {
            boolean result = RegexUtils.checkMobile("+8613620285733");
            Assert.assertTrue(result);
        }  

        /**
         * 验证电话号码
         */
        @Test
        public void testCheckPhone() {
            boolean result = RegexUtils.checkPhone("+860738-4630706");
            Assert.assertTrue(result);
        }  

        /**
         * 验证整数(正整数和负整数)
         */
        @Test
        public void testCheckDigit() {
            boolean result = RegexUtils.checkDigit("123132");
            Assert.assertTrue(result);
        }  

        /**
         * 验证小数和整数(正负整数和正负小数)
         */
        @Test
        public void testCheckDecimals() {
            boolean result = RegexUtils.checkDecimals("-33.2");
            Assert.assertTrue(result);
        }  

        /**
         * 验证空白字符
         */
        @Test
        public void testCheckBlankSpace() {
            boolean result = RegexUtils.checkBlankSpace("           ");
            Assert.assertTrue(result);
        }  

        /**
         * 匹配中文
         */
        @Test
        public void testCheckChinese() {
            boolean result = RegexUtils.checkChinese("中文");
            Assert.assertTrue(result);
        }  

        /**
         * 验证日期
         */
        @Test
        public void testCheckBirthday() {
            boolean result = RegexUtils.checkBirthday("1992/09/03");
            Assert.assertTrue(result);
        }  

        /**
         * 验证中国邮政编码
         */
        @Test
        public void testCheckPostcode() {
            boolean result = RegexUtils.checkPostcode("417100");
            Assert.assertTrue(result);
        }  

        /**
         * 验证URL地址
         */
        @Test
        public  void testCheckURL() {
            boolean result = RegexUtils.checkURL("http://blog.csdn.com:80/xyang81/article/details?name=&abc=中文");
            Assert.assertTrue(result);
        }  

        /**
         * 验证IP地址
         */
        @Test
        public void testCheckIpAddress() {
            boolean result = RegexUtils.checkIpAddress("192.1.22.255");
            Assert.assertTrue(result);
        }
    }  
时间: 2024-08-01 18:50:50

Android常用正则工具类的相关文章

Android常用的工具类

主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.SerializeUtils.S

Android中常用的工具类01

1.图片和视频缩略图工具类 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; /** * 缩略图生成工具类 * @author * */ public class ThumbnailGenerateUtils { private ThumbnailGenerateUtils(){}; /** * 根据指定的图像路径和大小来获取缩略图

Android中常用的工具类02

1.读取手机联系人信息 一般用在读取手机通讯录上传,这一块比较多. import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phon

android经常使用正则工具类

此类提供日常开发中经常使用的正则验证函数.比方:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); return m.matches(); 每一个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论. 以下是工具类的完整代码: 源代

java中常用的工具类(二)

下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

正则工具类以及FinalClass

package com.jadyer.server.util; import java.util.regex.Pattern; /** * 正则工具类 * @see final class可以提高执行速度,原因如下 * @see 1)不涉及继承和覆盖 * @see 2)其地址引用和装载在编译时完成,而不是在运行期间由JVM进行复杂的装载,因而简单并有效 * @see 3)运行时不要求JVM执行因覆盖而产生的动态地址引用而花费时间和空间 * @see 4)与继承链上的一般对象相比,垃圾回收器在收回

一个使用命令行编译Android项目的工具类

一个使用命令行编译Android项目的工具类 简介 编译apk项目需要使用的几个工具,基本都在sdk中,它们分别是(Windows系统): 1.aapt.exe 资源打包工具 2.android.jar Android编译工具 3.dx.bat dex文件生成工具 4.sdklib.jar 生成apk 5.jarsigner 签名工具 准备 在打包前,需要的环境如下: 1.JDK1.6+ 2.Android SDK 3.上述5个工具的路径 打包过程 1.生成R.java文件 比如: aapt p

common-lang 常用的工具类使用示例

原文:common-lang 常用的工具类使用示例 源代码下载地址:http://www.zuidaima.com/share/1550463718640640.htm common-lang 常用的工具类使用示例 StringUtil.dateUtil.DateFormatUtils.NumberUtils等 package com.zuidaima.trs.StringUtil; import java.io.File; import java.io.FileInputStream; imp

java中常用的工具类(三)

继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53