package com.fsti.icop.util.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern; public final class RegExpValidatorUtils {
/**
* 验证邮箱
*
* @param 待验证的字符串
* @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isEmail(String str) {
String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
return match(regex, str);
} /**
* 验证IP地址
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isIP(String str) {
String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
return match(regex, str);
} /**
* 验证网址Url
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsUrl(String str) {
String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
return match(regex, str);
} /**
* 验证电话号码
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsTelephone(String str) {
String regex = "^(\\d{3,4}-)?\\d{6,8}$";
return match(regex, str);
} /**
* 验证输入密码条件(字符与数据同时出现)
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPassword(String str) {
String regex = "[A-Za-z]+[0-9]";
return match(regex, str);
} /**
* 验证输入密码长度 (6-18位)
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPasswLength(String str) {
String regex = "^\\d{6,18}$";
return match(regex, str);
} /**
* 验证输入邮政编号
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPostalcode(String str) {
String regex = "^\\d{6}$";
return match(regex, str);
} /**
* 验证输入手机号码
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsHandset(String str) {
String regex = "^[1]+[3,5]+\\d{9}$";
return match(regex, str);
} /**
* 验证输入身份证号
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsIDcard(String str) {
String regex = "(^\\d{18}$)|(^\\d{15}$)";
return match(regex, str);
} /**
* 验证输入两位小数
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsDecimal(String str) {
String regex = "^[0-9]+(.[0-9]{2})?$";
return match(regex, str);
} /**
* 验证输入一年的12个月
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsMonth(String str) {
String regex = "^(0?[[1-9]|1[0-2])$";
return match(regex, str);
} /**
* 验证输入一个月的31天
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsDay(String str) {
String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
return match(regex, str);
} /**
* 验证日期时间
*
* @param 待验证的字符串
* @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isDate(String str) {
// 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],
// [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])
// String regex =
// "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";
// 没加时间验证的YYYY-MM-DD
// String regex =
// "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
// 加了时间验证的YYYY-MM-DD 00:00:00
String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
return match(regex, str);
} /**
* 验证数字输入
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsNumber(String str) {
String regex = "^[0-9]*$";
return match(regex, str);
} /**
* 验证非零的正整数
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsIntNumber(String str) {
String regex = "^\\+?[1-9][0-9]*$";
return match(regex, str);
} /**
* 验证大写字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsUpChar(String str) {
String regex = "^[A-Z]+$";
return match(regex, str);
} /**
* 验证小写字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLowChar(String str) {
String regex = "^[a-z]+$";
return match(regex, str);
} /**
* 验证验证输入字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLetter(String str) {
String regex = "^[A-Za-z]+$";
return match(regex, str);
} /**
* 验证验证输入汉字
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsChinese(String str) {
String regex = "^[\u4e00-\u9fa5],{0,}$";
return match(regex, str);
} /**
* 验证验证输入字符串
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLength(String str) {
String regex = "^.{8,}$";
return match(regex, str);
} /**
* @param regex
* 正则表达式字符串
* @param str
* 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
} // 3. 检查字符串重复出现的词
//
// private void btnWord_Click(object sender, EventArgs e)
// {
// System.Text.RegularExpressions.MatchCollection matches =
// System.Text.RegularExpressions.Regex.Matches(label1.Text,
//
// @"\b(?<word>\w+)\s+(\k<word>)\b",
// System.Text.RegularExpressions.RegexOptions.Compiled |
// System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// if (matches.Count != 0)
// {
// foreach (System.Text.RegularExpressions.Match match in matches)
// {
// string word = match.Groups["word"].Value;
// MessageBox.Show(word.ToString(),"英文单词");
// }
// }
// else { MessageBox.Show("没有重复的单词"); }
//
//
// }
//
// 4. 替换字符串
//
// private void button1_Click(object sender, EventArgs e)
// {
//
// string strResult =
// System.Text.RegularExpressions.Regex.Replace(textBox1.Text,
// @"[A-Za-z]\*?", textBox2.Text);
// MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + "替换的字符:" + "\n"
// + textBox2.Text + "\n" +
//
// "替换后的字符:" + "\n" + strResult,"替换");
//
// }
//
// 5. 拆分字符串
//
// private void button1_Click(object sender, EventArgs e)
// {
// //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁
// foreach (string s in
// System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*"))
// {
// textBox2.Text+=s; //依次输出 "甲乙丙丁"
// }
//
// } }

java 正则表达式验证的更多相关文章

  1. Java正则表达式验证IP,邮箱,电话

     引言     java中我们会常用一些判断如IP.电子邮箱.电话号码的是不是合法,那么我们怎么来判断呢,答案就是利用正则表达式来判断了,废话不多说,下面就是上代码. 1:判断是否是正确的IP  1 ...

  2. java正则表达式:验证字符串数字

    正则表达式:^([0-9]+)$ -> ^:匹配以0-9开头,[0-9]:匹配0-9数字,+:匹配至少一个数字,$:匹配以数字结尾 /** *正则表达式:验证字符串数字 *两种方式: *1.pa ...

  3. java 正则表达式 验证邮箱

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class demo1 { /**java正则表达式 * ...

  4. JS和Java正则表达式验证

    js代码 <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+ ...

  5. java 正则表达式验证邮箱格式是否合规 以及 正则表达式元字符

    package com.ykmimi.testtest; /** * 测试邮箱地址是否合规 * @author ukyor * */ public class EmailTest { public s ...

  6. java正则表达式验证邮箱、手机号码

    /** * 验证邮箱地址是否正确 * @param email * @return */ public static boolean checkEmail(String email){ boolean ...

  7. java正则表达式验证标点符号

    统计标点符号个数 String str = "\""..,!,"; int count = 0; Pattern pattern = Pattern.compi ...

  8. java正则表达式验证汉字

    统计指定内容的汉字个数: String str = "北京欢迎你 hello welcome!"; int count=0; Pattern pattern = Pattern.c ...

  9. java正则表达式验证金额

    String reg_money = "\\d+(\\.\\d{1,2})?";// 金额正则,可以没有小数,小数最多不超过两位 Pattern pattern = Pattern ...

随机推荐

  1. sublime php插件

    1. Package Control Get Package Control here. 2. Theme - Phoenix   and  Flatland(扁平) If Sublime Text ...

  2. 使用Scala编写Spark程序求基站下移动用户停留时长TopN

    使用Scala编写Spark程序求基站下移动用户停留时长TopN 1. 需求:根据手机基站日志计算停留时长的TopN 我们的手机之所以能够实现移动通信,是因为在全国各地有许许多多的基站,只要手机一开机 ...

  3. c#读excel 不用office组件

    某日笔者写了个导入导出excel的小程序,拿给别人用时,别人说程序有问题读不了. 究其原因发现原来是using Microsoft.Office.Interop.Excel;引用出错.发现他装的off ...

  4. CentOS7.3 搭建Openvpn

    环境:CentOS Linux release 7.3.1611 (Core) 查看os版本命令:[root@openvpn ~]# cat /etc/redhat-release 命令记录如下: y ...

  5. Linux RPM 命令参数使用详解 查看 rpm包依赖性

    转载自:http://blog.csdn.net/deutschester/article/details/6309521 rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两种 ...

  6. spring 在service中需要抛出异常才能自动回滚

    在spring 事务配置中,如果service方法捕获了异常,则程序报错也不会自动回滚, 1.手动开启关闭事务 2.抛出异常,可以先捕获异常,然后自定义runtime异常,可不用声明

  7. Session 活化与钝化 与tomcat钝化驱动器

    1,绑定到Session中的 对象有多种状态,分别是   绑定   解除绑定     钝化    活化. 绑定 是保存,通过session.setattribute把对象保存到session的对象当中 ...

  8. Frosh Week

    Problem Description During Frosh Week, students play various fun games to get to know each other and ...

  9. 基于CSS3自定义发光radiobox单选框

    之前我们分享过一些CSS3和HTML5实现的自定义checkbox和Radiobox,比如纯CSS3美化Checkbox和Radiobox按钮,不仅外观唯美,而且Radiobox选中时还有动画效果.今 ...

  10. netty5入门教程

    1.Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socke ...