且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-06-27 22:17:55

基本上,您需要采用3或4种不同的模式并将它们与|组合:

Basically, you need to take 3 or 4 different patterns and combine them with "|":

String pattern = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";




  • \d {10} 匹配1234567890

  • (?:\d {3} - ){2} \d {4} 匹配123-456-7890

  • \(\d {3} \)\\\ {3} - ?\d {4} 匹配(123)456-7890或(123)4567890

    • \d{10} matches 1234567890
    • (?:\d{3}-){2}\d{4} matches 123-456-7890
    • \(\d{3}\)\d{3}-?\d{4} matches (123)456-7890 or (123)4567890