gpt4 book ai didi

c# - 'generic' 电话号码的基本正则表达式

转载 作者:太空狗 更新时间:2023-10-29 22:03:07 25 4
gpt4 key购买 nike

我需要一个正则表达式(用于 ASP .NET 网站)来验证电话号码。它应该是灵活的,唯一的限制是:

  • 应至少为 9 位数字
  • 没有字母
  • 可以包含空格、连字符、单个 (+)

我已经搜索过 SO 和 Regexlib.com,但我得到的表达式有更多限制,例如英国电话或美国等

最佳答案

^\s*\+?\s*([0-9][\s-]*){9,}$

分解:

^           # Start of the string
\s* # Ignore leading whitespace
\+? # An optional plus
\s* # followed by an optional space or multiple spaces
(
[0-9] # A digit
[\s-]* # followed by an optional space or dash or more than one of those
)
{9,} # That appears nine or more times
$ # End of the string

我更喜欢用后一种方式来写正则表达式,因为这样更容易在以后阅读和修改;大多数语言都有一个需要为此设置的标志,例如C# 中的 RegexOptions.IgnorePatternWhitespace

关于c# - 'generic' 电话号码的基本正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com