C# 正则表达式验证电话号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8908976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:07:00  来源:igfitidea点击:

C# Regex to validate phone number

c#regex

提问by Derin

It would be great if someone could help me with a Regex for phone numbers. Following are the conditions:

如果有人可以帮助我使用电话号码的正则表达式,那就太好了。以下是条件:

  • If + is present, then it should be the first character
  • Allowed characters are numbers ( ) space - and .
  • Minimum of 6 numbers and max 12
  • ( , ) and space can come anywhere in the string
  • - shouldn't be the first and last character and shouldn't appear immediately after +, if + is present.
  • 如果 + 存在,那么它应该是第一个字符
  • 允许的字符是数字 () 空格 - 和 .
  • 最少 6 个号码,最多 12 个
  • ( , ) 和空格可以出现在字符串中的任何位置
  • - 不应该是第一个和最后一个字符,也不应该紧跟在 + 之后,如果 + 存在。

Here are some valid numbers:

以下是一些有效数字:

  • +93483227359
  • +1 703 335 65123
  • 34565464
  • 001 (703) 332-6261
  • +1703.338.6512
  • +934-83227359
  • (111)123-4567
  • 111-123-4567
  • +93483227359
  • +1 703 335 65123
  • 34565464
  • 001 (703) 332-6261
  • +1703.338.6512
  • +934-83227359
  • (111)123-4567
  • 111-123-4567

Thanks in advance

提前致谢

采纳答案by hsz

Try with:

尝试:

^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$

However it does not handle number counting

但是它不处理数字计数

回答by Dmitry

Not exactly the answer to your question, but for those who need to work with phone numbers, there is a .NET port of Google's libphonenumber: libphonenumber-csharp.

不完全是您问题的答案,但对于那些需要使用电话号码的人来说,Google 的libphonenumber有一个 .NET 端口:libphonenumber-csharp