提问者:小点点

只匹配标准10位数字或只在字符之间加上单破折号和空格连字符的正则表达式


我有一个正则表达式如下:

^(?!(?:[^-]*-){2}|(?:[^ ]* ){2})[- 0-9]{9,10}$

它是9到10位数字,只允许一个'-'和''(空格),但我想修改它,这将允许一个'-'和''只在字符之间(不是在开始和结束)。

目前,它也允许在开始和结束时使用一个'-'和''(空格)。

如有任何帮助,我们将不胜感激。


共1个答案

匿名用户

您可以将正则表达式表述为:

^(?![^-]*-[^-]*-)(?![^ ]*[ ][^ ]*[ ])\d(?:[ -]?\d){8,9}$

null

null

这是说:

^                         from the start of the number
    (?![^-]*-[^-]*-)      assert zero or one hyphens
    (?![^ ]*[ ][^ ]*[ ])  assert zero or one spaces
    \d                    match a single digit
    (?:                   which is then followed by
        [ -]?             an optional space or hyphen separator
        \d                and a digit
    ){8,9}                8 or 9 times (total 9 or 10 digits)
$                         end of the number