Regular expression to match phone numbers with country codes

前端 未结 5 1880
慢半拍i
慢半拍i 2021-01-16 06:37

This is the regular expression I use to match phone numbers like:

00 00 00 00 00
00 00 0 00 00 00 00
+00 0 00 00 00 00

(\\d{2}\\s+\\d{2}\\s+\\d{2}\\s+\\d{2}         


        
5条回答
  •  生来不讨喜
    2021-01-16 06:57

    Try this instead:

    \d\d(\s+\d\d){4}|(\d\d\s+\d\d\s+\d|\+\s+\d)\d\d(\s+\d\d){3}
    

    which means:

    \d\d(\s+\d\d){4}    // 00 00 00 00 00
    
    |                   // OR
    
    (                   // (
      \d\d\s+\d\d\s+\d  //    00 00 0
      |                 //    OR
      \+\s+\d           //    + 0
    )                   // )
    \d\d(\s+\d\d){3}    // 00 00 00 00
    

提交回复
热议问题