Java:正则表达式
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; //依次输出 "甲乙丙丁"
- // }
- //
- // }
- }
常用正则表达式:
- 常用正则表达式
- 匹配特定数字:
- ^[1-9]\d*$ //匹配正整数
- ^-[1-9]\d*$ //匹配负整数
- ^-?[1-9]\d*$ //匹配整数
- ^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
- ^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
- ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
- ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
- ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
- ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
- (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
- 评注:处理大量数据时有用,具体应用时注意修正
- 匹配特定字符串:
- ^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
- ^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
- ^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
- ^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
- ^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
- 用户名:/^[a-z0-9_-]{3,16}$/
- 密码:/^[a-z0-9_-]{6,18}$/
- 十六进制值:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/
- 电子邮箱:/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
- URL:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
- IP 地址:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
- HTML 标签:/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
- Unicode编码中的汉字范围:/^[u4e00-u9fa5],{0,}$/
- 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
- 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
- 匹配双字节字符(包括汉字在内):[^\x00-\xff]
- 评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
- 匹配空白行的正则表达式:\n\s*\r
- 评注:可以用来删除空白行
- 匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? />
- 评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
- 匹配首尾空白字符的正则表达式:^\s*|\s*$
- 评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
- 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
- 评注:表单验证时很实用
- 匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
- 评注:网上流传的版本功能很有限,上面这个基本可以满足需求
- 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
- 评注:表单验证时很实用
- 匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
- 评注:匹配形式如 0511-4405222 或 021-87888822
- 匹配腾讯QQ号:[1-9][0-9]{4,}
- 评注:腾讯QQ号从10000开始
- 匹配中国大陆邮政编码:[1-9]\d{5}(?!\d)
- 评注:中国大陆邮政编码为6位数字
- 匹配ip地址:\d+\.\d+\.\d+\.\d+
- 评注:提取ip地址时有用
- 网址(URL) [a-zA-z]+://[^\s]*
- IP地址(IP Address) ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
- 电子邮件(Email) \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
- QQ号码 [1-9]\d{4,}
- HTML标记(包含内容或自闭合) <(.*)(.*)>.*<\/\1>|<(.*) \/>
- 密码(由数字/大写字母/小写字母/标点符号组成,四种都必有,8位以上) (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$
- 日期(年-月-日) (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))
- 日期(月/日/年) ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2})
- 时间(小时:分钟, 24小时制) ((1|0?)[0-9]|2[0-3]):([0-5][0-9])
- 汉字(字符) [\u4e00-\u9fa5]
- 中文及全角标点符号(字符) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
- 中国大陆固定电话号码 (\d{4}-|\d{3}-)?(\d{8}|\d{7})
- 中国大陆手机号码 1\d{10}
- 中国大陆邮政编码 [1-9]\d{5}
- 中国大陆身份证号(15位或18位) \d{15}(\d\d[0-9xX])?
- 非负整数(正整数或零) \d+
- 正整数 [0-9]*[1-9][0-9]*
- 负整数 -[0-9]*[1-9][0-9]*
- 整数 -?\d+
- 小数 (-?\d+)(\.\d+)?
Java:正则表达式的更多相关文章
- java正则表达式
java正则表达式 1.Java正则表达式的语法与示例: http://baike.xsoftlab.net/view/207.html 2.Java 正则表达式: http://www.runo ...
- Java正则表达式入门——转自RUNOOB.COM
Java 正则表达式 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. Java正则表达式和Perl的是最为相似 ...
- Java 正则表达式详解
Java 提供了功能强大的正则表达式API,在java.util.regex 包下.本教程介绍如何使用正则表达式API. 正则表达式 一个正则表达式是一个用于文本搜索的文本模式.换句话说,在文本中搜索 ...
- 【转】详解Java正则表达式语法
(转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...
- java正则表达式【大全】
[正则表达式]文本框输入内容控制整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$& ...
- JAVA正则表达式:Pattern类与Matcher类详解(转)
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- JAVA正则表达式:Pattern类与Matcher类详解
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- Java 正则表达式[转载]
PS:转载自CSDN博客看上去很美 众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学 ...
- Java正则表达式的应用
在很多种情况下,我们都必须对字符串进行匹配,以便判断字符串的格式是否符合要求,对字符串中的内容进行提取.比如,我要从一段话aabdfe中,判断这段话是否有包含ab这个词,那么如果用if-else来判断 ...
- Java正则表达式实用教程
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.java.util.regex包主要包括以下三个类:Pattern.Matcher和PatternSynta ...
随机推荐
- cinder服务端的keystone认证机制
keystone在openstack中的地位 Keystone作为OpenStack中的身份管理与授权模块,主要实现系统用户的身份认证.基于角色的授权管理.其他OpenStack服务的地址发现和安全策 ...
- 设置grid高度
$("#jqxSalaryCalculation").jqxGrid({ height: $("#jqxTree").height() - 73 });
- VSCode调试配置
http://code.visualstudio.com/docs/editor/debugging#_launch-configurations VSCode内置Node.js运行时, 能调试jav ...
- 【BZOJ1570】[JSOI2008]Blue Mary的旅行 动态加边网络流
[BZOJ1570][JSOI2008]Blue Mary的旅行 Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决 ...
- 170117、spring解决乱码
spring解决乱码这个问题网上有很多解决方法,也可以关注本博客的文章,在此不再赘述, 今天推荐大家另外两种解决方法! 问题现象: 1.后台代码 2.前端界面 解决方法: 方法1:是在后台请求方法上加 ...
- Android Download机制详解(一)DocumentUI部分
在Android中Google为我们集成了一套十分便利的Download机制,用来下载网络上的资源文件.以此省去了我们编写和维护大量与Download相关的代码. 组成 Android中Downloa ...
- YOLO v1论文笔记
You Only Look Once:Unified, Real-Time Object Detection 论文链接:https://arxiv.org/abs/1506.02640 Homep ...
- Linux挂载:VMware tools for Linux安装
安装VMTools [root@localhost ~]# mkdir /mnt/cdrom [root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ [roo ...
- C++ 动态内存 栈堆
C++ 动态内存_w3cschool https://www.w3cschool.cn/cpp/cpp-dynamic-memory.html
- Java 语言基础之数组(一)
数组定义及格式: 数组: 同一种类型数据的集合, 就是一个容器 定义数组格式1: 元素类型[] 数组名 = new 元素类型[元素个数(即数组长度)]; 说明: 数组是一个容器.而容器属于一个实体,实 ...