从字符串中提取数字是非常常见的需求,用正则表达式也很简单,几行代码即可完成。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
string tmp="2bi3g4ik4-12mk3" //测试数据
string r = @"-?[0-9]+";
Regex reg = new Regex(r, RegexOptions.IgnoreCase | RegexOptions.Singleline,
TimeSpan.FromSeconds(3));
MatchCollection col = reg.Matches(tmp);
foreach (Match c in col)
{
Console.WriteLine(c.Groups[0].Value);
}