小编Duc*_*cky的帖子

用于验证电话号码的Java正则表达式

在下面的代码中,我试图让输出为电话号码的不同格式,如果它是有效的.我想出了一切,但第11行的Java正则表达式代码是字符串模式.

 import java.util.regex.*;

  public class MatchPhoneNumbers {
   public static void main(String[] args) {           
     String[] testStrings = {               
               /* Following are valid phone number examples */             
              "(123)4567890", "1234567890", "123-456-7890", "(123)456-7890",
              /* Following are invalid phone numbers */ 
              "(1234567890)","123)4567890", "12345678901", "(1)234567890",
              "(123)-4567890", "1", "12-3456-7890", "123-4567", "Hello world"};

             // TODO: Modify the following line. Use your regular expression here
              String pattern = "^/d(?:-/d{3}){3}/d$";    
             // current pattern recognizes any string of digits           
             // Apply regular expression to each test string           
             for(String inputString : testStrings) …
Run Code Online (Sandbox Code Playgroud)

java regex

11
推荐指数
3
解决办法
4万
查看次数

标签 统计

java ×1

regex ×1