直接看实例
对日期字符串提取期中的数字
1、使用match
string d = "12/5/2022 14:55:40"; var reg = new Regex(@"^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(.+)"); var ms = reg.Match(d); if (ms.Success) { Console.WriteLine(ms.Groups[1]); Console.WriteLine(ms.Groups[2]); Console.WriteLine(ms.Groups[3]); Console.WriteLine(ms.Groups[4]); Console.WriteLine("ok"); }
结果:
2、matches
string d = "12/5/2022 14:55:40"; foreach (Match item in Regex.Matches(d,@"\d+")) { Console.WriteLine(item.Value); }
结果