第一眼看到这个题的时候,我就认为应该是一个验证性的题目。
static void Main(string[] args)
{
string input = "987 123 4567";
Regex rx = new Regex(@"^[0−9]3[0−9]3 [0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$");
MatchCollection matchs = rx.Matches(input);
if (matchs.Count>0)
{
Console.WriteLine(matchs[0].Groups[0].Value);
}
Console.ReadKey();
}
当我看到这个题是这样解得时候我由衷的不想写上答案,因为我正则表达式研究的不是很深,更是没有系统的学习。
不过在查询正则的途中,也意外发现了Regex函数,并且了解到正则可以不仅仅用来验证,还能做一些替换等功能。总之,有必要学习一下。
在此模仿特朗普临时签署备忘录 navy_master