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地址,格式:zhangsan@sina.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkEmail(String email) {
String regex = "\\w+@\\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("zha2_ngsan@sina.com.cn");
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);
}
}
Android常用正则工具类的更多相关文章
- Android常用的工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...
- Android常用的工具类(转)
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- 2013最新Android常用的工具类整理
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pref ...
- Android常用的工具类SharedPreferences封装类SPUtils
package com.zhy.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect. ...
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- android经常使用正则工具类
此类提供日常开发中经常使用的正则验证函数.比方:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: ...
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- Android 软件管理工具类Utils
Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...
- 正则工具类以及FinalClass
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/27811103 完整版见https://jadyer. ...
随机推荐
- LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
题意:有一个有序序列A,其内部可能有部分被旋转了,比如A[1...n]被转成A[mid...n]+A[1...mid-1],如果被旋转,只有这种形式.问最小元素是?(假设没有重复元素) 思路:如果是序 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- json转csv
import re # csv格式 # 'k1,k2,k3\nv1,v2,v3\nv4,v5,v6\n' market_list_data = { "data": [ { &quo ...
- 【转】 IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- [Everyday Mathematics]20150111
设 $n$ 阶方阵 $A=(\al_1,\cdots,\al_n)$ 非奇异, $B=(0,\al_2,\cdots,\al_n)$. 试证: $BA^{-1}$, $A^{-1}B$ 的秩均为 $n ...
- [转载] python+Eclipse+pydev环境搭建
转自:http://www.cnblogs.com/Bonker/p/3584707.html 编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用. ...
- 《Python核心编程》 第四章 Python对象- 课后习题
练习 4-1. Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下. 答:身份.类型和值: 身份:每一个对象都有一个唯一的身份标识自己,可以用id()得到. 类型:对象的 ...
- g++安装 Gnome/Gtk+开发库 的 环境安装(安装widgets的必要条件)
sudo yum install gcc-c++ 我在更新软件源后,看了看最新版的GTK开发版是3.0的. 所以,基本可以照搬. sudo yum -t install gtk3 sudo yum - ...
- spoj 839 Optimal Marks(二进制位,最小割)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...
- HDU 1536 sg-NIM博弈类
题意:每次可以选择n种操作,玩m次,问谁必胜.c堆,每堆数量告诉. 题意:sg—NIM系列博弈模板题 把每堆看成一个点,求该点的sg值,异或每堆sg值. 将多维转化成一维,性质与原始NIM博弈一样. ...