Java常用正则表达式验证类完整实例【邮箱、URL、IP、电话、身份证等】

作者:hacker_Lees 时间:2022-09-14 05:59:39 

本文实例讲述了Java常用正则表达式验证类。分享给大家供大家参考,具体如下:


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; //依次输出 "甲乙丙丁"
// }
//
// }
}

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述对大家java程序设计有所帮助。

来源:https://blog.csdn.net/hacker_Lees/article/details/80280418

标签:Java,正则表达式
0
投稿

猜你喜欢

  • Laravel框架实现点播上传阿里云功能

    2023-06-13 20:13:30
  • mysql判断当前时间是否在开始与结束时间之间且开始与结束时间允许为空

    2024-01-23 17:35:43
  • Linux下 php7安装redis的方法

    2024-05-13 09:54:05
  • Centos7 mysql数据库安装及配置实现教程

    2024-01-16 06:39:40
  • 深入理解go slice结构

    2024-04-26 17:27:07
  • Python偏函数Partial function使用方法实例详解

    2023-02-07 09:47:02
  • 阿里云ECS centos6.8下安装配置MySql5.7的教程

    2024-01-14 23:47:13
  • Centos7 Python3下安装scrapy的详细步骤

    2021-12-03 10:59:26
  • 浅析python 内置字符串处理函数的使用方法

    2021-07-17 09:23:09
  • Python实现完全数的示例详解

    2021-11-21 20:09:30
  • python检查字符串是否是正确ISBN的方法

    2022-05-10 14:54:01
  • tensorboard实现同时显示训练曲线和测试曲线

    2023-05-18 04:55:34
  • SQL 字母数字混合型字段 按里面的数字排序

    2010-04-23 18:18:00
  • asp如何使用Office Chart 9.0 制作图表?

    2010-06-05 12:41:00
  • Python通过Django实现用户注册和邮箱验证功能代码

    2021-02-21 13:59:03
  • Python上传package到Pypi(代码简单)

    2022-04-21 17:09:33
  • golang 比较浮点数的大小方式

    2024-04-28 10:48:50
  • 恢复被删除的数据 Log Explorer for SQL Server 4.2 (一)

    2010-07-01 19:24:00
  • ASP使用wsImage组件给图片加水印代码

    2010-06-09 19:23:00
  • DB2和 Oracle的并发控制(锁)的比较

    2009-02-28 10:29:00
  • asp之家 网络编程 m.aspxhome.com