正则表达式

目录

一.正则表达式的常见应用场景

二.推荐几款正则表达式的测试工具

三.基础语法

1.元字符

2.反义符

3.边界符

4.转义字符

 5.计量符

 6.逻辑符

 7.贪婪与懒惰语法

 8.正则表达式处理选项(RegexOptions)

四.常用正则表达式


一.正则表达式的常见应用场景

  • 批量提取、替换有规律的字符串在各种高级文本编辑器中的使用
  • 在各类办公软件(Office等)中使用
  • 各种开发语言中的使用(C#、Java、JS、Perl、PHP等)
  • 用户输入的合法性验证(IP地址、特殊的订单号要求等)
  • 模板引擎的标签库开发
  • 网络爬虫(抓取机器人)的开发
  • 批量的文本高效处理

二.推荐几款正则表达式的测试工具

1.推荐RegexBuddy
http: //www.regexbuddy. com

2.RegExBuilder
http://www.redfernplace.com/software-projects/regex-builder/

3.在线测试工具

  • http://tool.chinaz.com/regex/
  • http://regexper. com
  • https://www. debuggex. com

三.基础语法

1.元字符

字符名称说明
.匹配除换行符以外的任意字符
\wword单词匹配字母、数字、下划线和汉字
\sSpace空格匹配任意的空格字符
\dDigit数字匹配数字
\bBreak打破,单词边界匹配单词的开始或结束

2.反义符

字符说明
\W匹配任意不是字符、数字、下划线、汉字的字符
\S匹配任意不是空格符的字符
\D匹配任意非数字的字符
\B匹配不是单词开头或结束的位置

3.边界符

字符说明
^匹配字符串的开始
$匹配字符串的结束
[x]匹配单个字符,x代表任意单个字符
()表示分组
-表示区间

4.转义字符

 

字符说明
\x表示正则表达式中占用的任意特殊字符
\\匹配转义字符\本身
\t匹配一个制表符
\r匹配一个Enter符
\n匹配一个换行符

 5.计量符

字符说明
*匹配字符(贪婪)重复零次或更多(任意次数)
+匹配字符(懒惰)重复一次或更多次(至少重复一次)
?匹配字符(占位)重复零次或一次(可有可无)
{n}匹配字符重复n次
{n , m}匹配字符重复n次到m次
{n, }表示某个字符表示区间

 6.逻辑符

字符说明
|表示逻辑或
=逻辑等于(环视肯定顺序)
!逻辑非(环视否定顺序)
<=环视肯定逆序
<!环视否定逆序

 7.贪婪与懒惰语法

语法说明
*?重复任意次,但尽可能少重复
=?重复1次或更多次,但尽可能少重复
??重复0次或1次,但尽可能少重复
{n, m}?重复n到m次,但尽可能少重复
{n, }重复n次以上,但尽可能少重复

 8.正则表达式处理选项(RegexOptions)

语法说明
lgnoreCase匹配时不区分大小写
Multiline更改^和$的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。(在此模式下,$的精确含义是:匹配\n之前的位置以及字符串结束前的位置。)
Singleline更改的含义,使它与每一个字符匹配(包括换行符\n)
lngnorePatternWhitespace忽略表达式中的非转义空白并启用由#标记的注释
ExplicitCapture仅捕获已被显式命名的组

四.常用正则表达式

package com.ws.strategy.promition;

import java.util.regex.Pattern;


/**
 * 常用正则表达式
 */
public class RegexUtils {
	
	private RegexUtils(){}

	/**
	 * 整数
	 */
	public static final String intege = "^-?[1-9]\\d*$/";					//整数
	/**
	 * 正整数
	 */
	public static final String intege1 = "^[1-9]\\d*$/";					//正整数
	/**
	 * 负整数
	 */
	public static final String intege2 = "^-[1-9]\\d*$/";					//负整数
	/**
	 * 数字
	 */
	public static final String num = "^([+-]?)\\d*\\.?\\d+$/";			//数字
	/**
	 * 正数(正整数 + 0)
	 */
	public static final String num1 = "^[1-9]\\d*|0$/";					//正数(正整数 + 0)
	/**
	 * 负数(负整数 + 0)
	 */
	public static final String num2 = "^-[1-9]\\d*|0$/";					//负数(负整数 + 0)
	/**
	 * 浮点数
	 */
	public static final String decmal = "^([+-]?)\\d*\\.\\d+$/";			//浮点数
	/**
	 * 正浮点数
	 */
	public static final String decmal1 = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$"; //正浮点数
	/**
	 * 负浮点数
	 */
	public static final String decmal2 = "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$"; //负浮点数
	/**
	 * 浮点数
	 */
	public static final String decmal3 = "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$";//浮点数
	/**
	 * 非负浮点数(正浮点数 + 0)
	 */
	public static final String decmal4 = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$";  //非负浮点数(正浮点数 + 0)
	/**
	 * 非正浮点数(负浮点数 + 0)
	 */
	public static final String decmal5 = "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$"; //非正浮点数(负浮点数 + 0)

	/**
	 * 邮件
	 */
	public static final String email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$"; //邮件
	/**
	 * 颜色
	 */
	public static final String color = "^[a-fA-F0-9]{6}$";				//颜色
	/**
	 * url
	 */
	public static final String url = "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$";	//url
	/**
	 * 仅中文
	 */
	public static final String chinese = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";					//仅中文
	/**
	 * 仅ACSII字符
	 */
	public static final String ascii = "^[\\x00-\\xFF]+$";				//仅ACSII字符
	/**
	 * 邮编
	 */
	public static final String zipcode = "^\\d{6}$";						//邮编
	/**
	 * 手机
	 */
	public static final String mobile = "^(13|15|16|18)[0-9]{9}$";				//手机
	/**
	 * ip地址
	 */
	public static final String ip4 = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";	//ip地址
	/**
	 * 非空
	 */
	public static final String notempty = "^\\S+$";						//非空
	/**
	 * 图片
	 */
	public static final String picture = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga|JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA)$";	//图片
	
	/**
	 * 音频
	 */
	public static final String audio = "(.*)\\.(mp3|wma|mid|midi|wav|vqf|MP3|WMA|MID|MIDI|WAV|VQF)$";	//音频
	
	/**
	 * 视频
	 */
	public static final String video = "(.*)\\.(rm|3gp|mp4|rmvb|avi|wmv|flv|vob|exe|mkv|swf|RM|3GP|MP4|RMVB|AVI|WMV|FLV|VOB|EXE|MKV|SWF)$"; // 视频格式
	
	/**
	 * 压缩文件
	 */
	public static final String rar = "(.*)\\.(rar|zip|7zip|tgz|RAR|ZIP|7ZIP|TGZ)$";	//压缩文件
	/**
	 * 日期
	 */
	public static final String date = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$";	//日期
	/**
	 * 日期时间
	 */
	public static final String datetime = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}(\\s\\d{2}:)?(\\d{2}:)?(\\d{2})?$";	//日期和时间
	/**
	 * QQ号码
	 */
	public static final String qq = "^[1-9]*[1-9][0-9]*$";		//QQ号码
	/**
	 * 电话号码的函数(包括验证国内区号,国际区号,分机号)
	 */
	public static final String tel = "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$";	//电话号码的函数(包括验证国内区号,国际区号,分机号)
	/**
	 * 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
	 */
	public static final String username = "^[A-Za-z]\\w{5,}$";	//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
	/**
	 * 字母数字组合
	 */
	public static final String allstring = "^\\w+$"; //字母数字组合
	/**
	 * 字母
	 */
	public static final String letter = "^[A-Za-z]+$";					//字母
	/**
	 * 大写字母
	 */
	public static final String letter_u = "^[A-Z]+$";					//大写字母
	/**
	 * 小写字母
	 */
	public static final String letter_l = "^[a-z]+$";					//小写字母
	/**
	 * 第二代身份证
	 */
	public static final String idcard = "^[1-9]([0-9]{14}|[0-9]{17})$";	//身份证
	
	/**
	 * 数字或字母
	 */
	public static final String numOrStr = "^[A-Za-z0-9]+$";//数字或字母
	/**
	 * 匹配字符是否符合要求
	 * @return
	 */
	public static boolean test(String input,String regx)
	{
		return Pattern.matches(regx, input);
	}
	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w7486

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值