好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java正则表达式验证固定电话号码符合性

下面给大家介绍java正则表达式验证固定电话号码符合性,具体代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

/**

  * 验证固定电话号码的合法性

  * @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

?

1

2

3

4

5

6

7

8

public boolean isnumeric(string str){

   pattern pattern = pattern.compile( "[0-9]*" );

   matcher isnum = pattern.matcher(str);

   if ( !isnum.matches() ){

     return false ;

   }

   return true ;

}

PS:推荐2款在线正则表达式工具,有需要的朋友可以看看

正则表达式在线测试工具  https://tool.tuohang.net/t/regex/

正则表达式生成器  https://tool.tuohang.net/t/regcode/

总结

以上所述是小编给大家介绍的java正则表达式验证固定电话号码符合性,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://blog.csdn.net/weixin_41888813/article/details/82492220

查看更多关于Java正则表达式验证固定电话号码符合性的详细内容...

  阅读:49次