如何提取格式为hh:mm:ss的子字符串.SSSZ从任何字符串

本文关键字:字符串 ss mm SSSZ 任何 hh 何提取 提取 格式 | 更新日期: 2023-09-27 17:51:22

例如,我有字符串(不固定大小)

"mynewtime10:20:13.458atcertainplace"    
"hertimeatthatplace11:20:55.12nocomment"

时间在不同的位置(索引)从一个字符串到另一个

如何提取格式为hh:mm:ss的子字符串.SSSZ从任何字符串

试一下这段代码,希望能成功。

    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组