常用正则表达式全集

1.常用元字符

代码说明
.匹配除换行符以外的任意字符
\w匹配字母或数字或下划线
\s匹配任意的空白符
\d匹配数字
\b匹配单词的开始或结束
^匹配字符串的开始
$匹配字符串的结束

2. 常用限定符

代码/语法说明
*重复零次或更多次
+重复一次或更多次
?重复零次或一次
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次

3. 常用反义词

代码/语法说明
\W匹配任意不是字母,数字,下划线,汉字的字符
\S匹配任意不是空白符的字符
\D匹配任意非数字的字符
\B匹配不是单词开头或结束的位置
[^x]匹配除了x以外的任意字符
[^aeiou]匹配除了aeiou这几个字母以外的任意字符

4. 逻辑符

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

5. 常用正则表达式

/**
	 * 整数
	 */
	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)
	 */
	public static final String num2 = "^-[1-9]\\d*|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)
	 */
	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-.\\/?%&=]*)?$";	
	
	/**
	 * 仅中文
	 */
	public static final String chinese = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";					
	
	/**
	 * 仅ACSII字符
	 */
	public static final String ascii = "^[\\x00-\\xFF]+$";				
	
	/**
	 * 邮编
	 */
	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)$";	
	
	/**
	 * 非空
	 */
	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]*$";		
	
	/**
	 * 电话号码的函数(包括验证国内区号,国际区号,分机号)
	 */
	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,}$";	
	
	/**
	 * 字母数字组合
	 */
	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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值