美国免费电话号码1 800 ###-####
的正则表达式是什么?如何使用c#验证这个电话?
答案 0 :(得分:2)
你可以试试这个
^(\+?1)?(8(00|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|55|66|77|88 - matches 00 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);