例如,我有字符串(不固定大小)
"mynewtime10:20:13.458atcertainplace"
"hertimeatthatplace11:20:55.12nocomment"
时间在不同的位置(索引)从一个字符串到另一个
试一下这段代码,希望能成功。
using System.Text.RegularExpressions;
static void Main(string[] args)
{
string str = "mynewtime10:20:13.458atcertainplace";
string patt = @"([0-9:.]+)";
Regex rgx = new Regex(patt, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(str);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", str, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + match.Value);
}
Console.ReadLine();
}
也许使用以下正则表达式?
([0-9:.]+)
您搜索的字符串在第1组