C# 美国免费电话号码的正则表达式

C# 美国免费电话号码的正则表达式,c#,regex,C#,Regex,美国免费电话号码的正则表达式是什么?如何使用c#?对手机进行验证您可以试试这个 ^(\+?1)?(8(00|33|44|55|66|77|88)[2-9]\d{6})$ 解释 ^ - indicates the beginning of a regular expression (required); ( - indicates the beginning of a regular expression block - blocks are important to define i

美国免费电话号码的正则表达式是什么?如何使用c#?

对手机进行验证您可以试试这个

^(\+?1)?(8(00|33|44|55|66|77|88)[2-9]\d{6})$
解释

^ - indicates the beginning of a regular expression (required);
( - indicates the beginning of a regular expression block - 
    blocks are important to define inner expressions so they can be referenced by the variables $1, $2, $3, etc;
\+1|1? - indicates optional digits '+1' or '1' (the ? sign defines the literal as optional);
) - closes the block;
8 - matches the literal '8';
( - another block starting;
00|33|44|55|66|77|88 - matches 00 or 33 or 44 or 55 or 66 or 77 or 88 (you guessed right, the | sign is the OR operator);
) - closes the inner block;
[2-9] - matches one digit in the range from 2 to 9 (2, 3, 4, 5, 6, 7, 8 and 9), 
    and as you also guessed the [] pair of brackets encloses a range; 
    other range examples: [0-9] matches 0 to 9; [a-z] matches a, b, c, ...z);
\d - matches any valid digit (same as [0-9]);
{6} - defines the number of occurrences for the previous expression, 
    i.e. exactly 6 digits in the range 0-9. This can also contain a variable number of occurrences, 
    for example to match a sequence of 6 to 9 digits: {6,9}; 
    or to match at least 6 with no maximum: {6,};
) - closes the other block;
$ - indicates the end of the regular expression (required);

到目前为止你做了什么?实际上我不会做正则表达式。那你应该开始学习正则表达式了。人们做这些事情是有理由得到报酬的。除此之外,Mohit给出的解决方案确实有效。实际上,您可以将它写得更小:
^((?:\+-1)?8[04-8]{2}[2-9]\d{6})$
@GottZ较小的表达式错误地将8565551212报告为免费数字,因为[04-8]{2}部分不需要重复第一个字符。它允许[04-8]中的任何一个后面跟[04-8]中的任何一个。@BillMenees hm。那么就不那么短了:
^(+\+-1)?(8([03-8])\3[2-9]\d{6}$