111,096
社区成员




代码如下:
static void Main(string[] args)
{
string pattern = @"1\d{10}"; // 匹配中国大陆手机号,以1开头,共11位
string input = "Here is a phone number: 13812345678 and a bank card number: 15515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine("找到手机号: " + match.Value);
}
Console.ReadKey();
}
真正的手机号只有一个,但是程序会把后面一串数字也提取成切割成手机号,如何规避?
// 正反向预查, 数字前后不能是数字
string pattern = @"(?<=\D)1\d{10}(?=\D)"; // 匹配中国大陆手机号,以1开头,共11位
改一下正则表达式,多匹配一下手机号后面的一个空格。
```c#
string pattern=@"1\d{10}\D";
```