小猿圈java之正则表达式

java的正则在java应用中经常会遇到,说到正则是java的重中之重,挺重要的一个知识点,小猿圈详细阐述一下java正则的几种用法,想要了解的,可以看一下小编的文章。

以下是正则模板:

常用正则表达式

匹配特定数字:

^[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+)?

详细事例说一下:

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

// }

//

// }

}

大家学会了吗?这篇对正则深入剖析了一下,讲的很细很明白,希望大家看后真正学到了,小编的目的也就达到了,如果感觉不错的,可以在小猿圈看一下其他的讲解,希望你会收获更多。

转载于:https://my.oschina.net/u/4144187/blog/3064737

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值