您的位置 首页 java

Java正则表达式大全详解「收藏起来以后你一定会用的到的」

片头:

正则表达式 ,又称规则表达式 (英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。

邮箱验证

/**
* 验证邮箱
* 
* @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地址

/**
* 验证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

/**
* 验证网址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位)

/**
* 验证输入密码长度 (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);
}
 

验证输入身份证号

End 喜欢的请 关注小编 哦,后续还更多干货分享,敬请期待!

片尾:

大家觉得文章内容好的话记得帮小编【 点个赞 】,您的点赞是我发文最大的动力!

喜欢的可以多多收藏,分享和转发,非常感谢!

喜欢我的文章可以【 关注小编 】哦,感谢大家!

如果文章有不正之处 请大家评论批评指正 我会看心情改 也有可能不会改!

尘世中一个浪荡的程序员 只为升级打怪! 我的愿望是世界和平!

Java学习资料获取方式: 关注小编,私信【你好世界】手机用户可以直接私信,电脑端尚未开放此功能,还需下载app,然后私信回复:【你好世界】 我已经设置了自动回复,请根据回复内容操作。

文章来源:智云一二三科技

文章标题:Java正则表达式大全详解「收藏起来以后你一定会用的到的」

文章地址:https://www.zhihuclub.com/185635.shtml

关于作者: 智云科技

热门文章

网站地图