- 浏览: 16380 次
- 性别:
- 来自: 北京
最新评论
import java.util.regex.*; public final class RegExpValidator { /** * 验证邮箱 * @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; //依次输出 "甲乙丙丁" // } // // } }
发表评论
-
数字金额转换成大写汉字,未完善版
2011-04-02 12:53 870自己练习数组而已,以后完善。 package com.tes ... -
几个常见数据库JDBC的连接信息
2011-03-02 13:52 1010示例: SQL Server2005:DB_DRIVER = ... -
MyStack的2个应用
2011-02-24 02:00 639复习栈的两个应用: 单词逆序: package com.t ... -
MyStack
2011-02-23 15:30 869复习栈,用数组实现的栈及测试: package com.te ... -
java实现栈
2011-02-12 17:05 0今天看书看到线程synchronized关键词时, ... -
线程的join()理解
2011-02-12 11:12 714查看了网上的资料,简单来说,join()的理解就是 ...
相关推荐
正则表达式正则表达式正则表达式正则表达式 验证程序
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
正则表达式验证金额格式
正则表达式验证工具源码 源码的简单描述: ...正则表达式验证工具,通过System.Text.RegularExpressions里边的Regex类的方法对输入的正则表达式与填写的字符串进行验证,界面干净简洁,功能实用,代码简单易懂。
c#使用正则表达式验证身份证号码,工程完全可以运行,方便大家参考和学习
正则表达式 学习 全收集 chm pdf 正则表达式验证工具 exe正则表达式 学习 全收集 chm pdf 正则表达式验证工具 exe正则表达式 学习 全收集 chm pdf 正则表达式验证工具 exe正则表达式 学习 全收集 chm pdf 正则表达式...
js表单输入正则表达式验证代码
邮箱及密码的正则表达式验证(C#)
使用vs2008写的一个正则表达式验证工具,附带源码,供使用c++写正则表达式参考使用,使用boost_1_54 regex库。
java 使用正则表达式验证电话号码的格式 java 使用正则表达式验证电话号码的格式
使用正则表达式验证一年的12个月份,程序完全可以运行,方便大家调试开发
正则表达式验证格式
jq非空验证,为空的时候input后面加红色字体的用户名不能为空。 js正则表达式验证邮箱和手机号码,验证邮箱必须含有@符,手机号码必须1开头并且固定11个字符
一个正则表达式验证格式的方法 你一个可以调用的demo valid(str,tostr) str:字符串 tostr:格式 yy-mm-dd 支持.,/,-三种格式。
本文实例讲述了微信小程序实现简单input正则表达式验证功能。分享给大家供大家参考,具体如下: 1、效果展示 2、关键代码 index.wxml文件 <input placeholder="输入内容" bindinput="check"></input> ...
使用正则表达式验证中文汉字输入,程序完全可以运行调试,方便大家参考
如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一...
常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集常用正则表达式收集
C#,VS2010,利用正则表达式验证网址(url),
全自动正则表达式验证工具