C#正则表达式截取字符串的一段字符串内容

1.测试工具

推荐大家用【RegexTester.exe】这个工具进行测试

C#正则表达式截取字符串的一段字符串内容

2.C#代码

从cmdStr这段字符串中截取 【Duration: 00:02:06.86,】里面的时长字符串

 
 string [email protected]"{nifubdbfDuration: 00:02:06.86, start: 0.000000, bitrate: 20355 kb/s    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/bt470bg/smpte170m), 1920x1080, 20208 kb/s, SAR 1:1 DAR 16:9, 29.99 fps, 30 tbr, 90k tbn, 180k tbc (default)    Metadata:      rotate          : 90      creation_time   : 2019-03-13T07:06:18.000000Z      handler_name    : VideoHandle    Side data:}"
 Regex regex = new Regex("Duration:(.*)start", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
        //表格: 0:Duration: 00:02:06.86, start   1: 00:02:06.86,  
        string dateStr = regex.IsMatch(cmdStr) ? regex.Match(cmdStr).Groups[1].ToString() : "";