Java正则表达式验证固定电话号码符合性
作者:Ares_行者 时间:2023-02-26 17:12:12
下面给大家介绍Java正则表达式验证固定电话号码符合性,具体代码如下所示:
/**
* 验证固定电话号码的合法性
* @author jy
*/
package phone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PhoneTest {
public static boolean isPhone(String str) {
Pattern p1 = null, p2 = null;
Matcher m = null;
boolean isPhone = false;
p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
if (str.length() > 9) {
m = p1.matcher(str);
isPhone = m.matches();
} else {
m = p2.matcher(str);
isPhone = m.matches();
}
return isPhone;
}
public static void main(String[] args) {
String phone = "0770-88889999";
if(isPhone(phone)){
System.out.println(phone+"是符合的电话号码");
}else {
System.out.println(phone+"不符合");
}
}
}
下面看下用正则表达式判断一个字符串是否全是数字
用正则表达式首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
总结
以上所述是小编给大家介绍的Java正则表达式验证固定电话号码符合 * 的支持!
来源:https://blog.csdn.net/weixin_41888813/article/details/82492220
标签:java,正则表达式,验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
只用40行Python代码就能写出pdf转word小工具
2021-11-03 18:02:00
![](https://img.aspxhome.com/file/2023/1/97501_0s.png)
基于Jquery+Ajax+Json实现分页显示附效果图
2024-05-21 10:12:27
![](https://img.aspxhome.com/file/2023/0/126150_0s.gif)
Mysql主从复制注意事项的讲解
2024-01-28 10:57:32
Go slice切片make生成append追加copy复制示例
2024-02-13 11:48:29
![](https://img.aspxhome.com/file/2023/6/109676_0s.jpg)
10种检测Python程序运行时间、CPU和内存占用的方法
2023-08-04 16:28:14
![](https://img.aspxhome.com/file/2023/3/61743_0s.jpg)
Python把对应格式的csv文件转换成字典类型存储脚本的方法
2022-07-08 08:39:06
![](https://img.aspxhome.com/file/2023/0/75190_0s.jpg)
使用c#构造date数据类型
2024-01-15 22:19:15
Pytorch中Softmax和LogSoftmax的使用详解
2022-09-24 07:36:19
Python正规则表达式学习指南
2021-04-11 15:21:16
![](https://img.aspxhome.com/file/2023/7/68827_0s.png)
Python实现视频目标检测与轨迹跟踪流程详解
2021-12-27 06:48:37
![](https://img.aspxhome.com/file/2023/3/118543_0s.png)
深入了解SQL Server 2008 商业智能平台
2009-01-15 13:03:00
详细解读Python的web.py框架下的application.py模块
2021-06-24 22:28:47
pycharm部署、配置anaconda环境的教程
2022-08-23 11:50:54
![](https://img.aspxhome.com/file/2023/6/101426_0s.jpg)
Go Generate 代替 Makefile使用方法详解
2024-04-27 15:28:18
python正则表达式中的括号匹配问题
2023-07-26 01:36:24
设计模式-自动完成
2010-11-30 21:44:00
![](https://img.aspxhome.com/file/UploadPic/201011/30/auto-complete-pic011-76s.jpg)
BootStrop前端框架入门教程详解
2024-04-29 13:46:10
![](https://img.aspxhome.com/file/2023/7/132537_0s.png)
详解JavaScript基于面向对象之继承
2024-04-29 13:23:06
![](https://img.aspxhome.com/file/2023/6/132676_0s.png)
微信小程序自定义底部导航带跳转功能
2024-05-02 17:25:49
![](https://img.aspxhome.com/file/2023/1/129781_0s.jpg)
mysql数据插入覆盖和时间戳的问题及解决
2024-01-21 02:15:26
![](https://img.aspxhome.com/file/2023/4/81044_0s.jpg)