Regular expression to match phone numbers with country codes

前端 未结 5 1881
慢半拍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:52

    1. Where you have your alternative formats listed like:

      (...)\s+(...)\s+(...)
      

      Change that to use the | (OR) operator:

      (...)|(...)|(...)
      
    2. Don't escape the parentheses. \( and \) should be simply ( and ).

    3. In your third group the + at the beginning should be escaped with a backslash:

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

提交回复
热议问题