正则表达式允许带有国家/地区代码的电话号码

Regex to allow PhoneNumber with country code

提问人:Viku 提问时间:4/12/2015 最后编辑:Viku 更新时间:4/12/2015 访问量:6644

问:

我需要一个正则表达式来验证电话号码(带国家/地区代码),该号码应遵循以下条件

1 - There should be at max 4 digits in between + and - . 
2 - Phone number shall be a combination of +,- and digits
3 - 0 shall not be allowed after - 
4 - After - only 10 digits are allowed

例如

    1 - +91234-1234567 - Fail (1st Condition fails)
    2 - +9123-1234567  - Pass 
    3 - +               - Fail (2nd condition fails)
    4 - -               - Fail (2nd condition fails)
    5 - 91234545555     - Fail (2nd condition fails)
    6 - +91-012345      - Fail (3rd Condition fails)
    7 - +91-12345678910 - Fail (4th condition fails)

请帮帮我.提前致谢。

JavaScript C# 正则表达式

评论


答:

2赞 jdphenix 4/12/2015 #1
\+\d{1,4}-(?!0)\d{1,10}\b

故障:

\+                        Match a literal +
\d{1,4}                   Match between 1 and 4 digits inclusive
-                         Match a literal -
(?!                       Negative lookahead, fail if
  0                       this token (literal 0) is found
)
\d{1,10}                  Match between 1 and 10 digits inclusive
\b                        Match a word boundary

演示(附示例)

var phoneRegexp = /\+\d{1,4}-(?!0)\d{1,10}\b/g,
  tests = [
    '+91234-1234567',
    '+9123-1234567',
    '+',
    '-',
    '91234545555',
    '+91-012345',
    '+91-12345678910'
  ],
  results = [],
  expected = [false, true, false, false, false, false, false];

results = tests.map(function(el) {
  return phoneRegexp.test(el);
});

for (var i = 0; i < results.length; i++) {
  document.getElementById('result').textContent += (results[i] === expected[i]) + ', ';
}
<p id="result"></p>

评论

0赞 jdphenix 4/12/2015
据我所知,除了知识和学习之外,没有一种工具可以生成正则表达式。对于大多数正则表达式,我通常会找到一个众所周知的、经过充分测试的正则表达式来匹配已知格式。
0赞 Zohar Peled 4/12/2015
使用前瞻而不是 view 有什么好处吗?\+\d{1,4}-[1-9]\d{,9}\b
0赞 jdphenix 4/12/2015
@vivek 试试看,不要看为什么不。
0赞 jdphenix 4/12/2015
@ZoharPeled我怀疑这会执行得更快。我使用 lookahead 是因为它更接近要求“0 不应允许在 - 之后”的语义\+\d{1,4}-[1-9]\d{1,9}\b
1赞 Bojan Komazec 4/12/2015 #2

在 C# 中,您可以使用:

public class RegexTelephoneNumber
{
    public void Test()
    {
        Regex regex = new Regex(@"^\+\d{1,4}-[1-9]\d{0,9}$");

        Trace.Assert(MatchTest(regex, "+91234-1234567") == false);
        Trace.Assert(MatchTest(regex, "+9123-1234567") == true);
        Trace.Assert(MatchTest(regex, "+") == false);
        Trace.Assert(MatchTest(regex, "-") == false);
        Trace.Assert(MatchTest(regex, "91234545555") == false);
        Trace.Assert(MatchTest(regex, "+91-012345") == false);
        Trace.Assert(MatchTest(regex, "+91-12345678910") == false);

        Trace.Assert(MatchTest(regex, "++91234-1234567") == false);
        Trace.Assert(MatchTest(regex, "+91234-1234567+") == false);
        Trace.Assert(MatchTest(regex, "aa+91234-1234567+bb") == false);
        Trace.Assert(MatchTest(regex, "+91-12") == true);
    }

    private bool MatchTest(Regex regex, string text)
    {
        Match match = regex.Match(text);
        return match.Success;
    }
}

正则表达式解释道:

^        - start anchor
\+       - character '+'
\d{1,4)  - any digit repeating min 1 and max 4 times
-        - character '-'
[1-9]    - any digit apart from 0, taking place exactly once
\d{0,9}  - any digit repeating min 0 and max 9 times
$        - end anchor