简体   繁体   English

正则表达式从字符串中拆分并提取多个部分

[英]regex split and extract multiple parts from a string

I am trying to extract some parts of the "Video:" line from below text. 我正在尝试从文本下方提取“视频:”行的某些部分。

Seems stream 0 codec frame rate differs from container frame rate: 30000.00 (300
00/1) -> 14.93 (1000/67)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\a.3gp':
  Metadata:
    major_brand     : 3gp5
    minor_version   : 0
    compatible_brands: 3gp5isom
  Duration: 00:00:45.82, start: 0.000000, bitrate: 357 kb/s
    Stream #0.0(und): Video: mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb
/s, 14.93 fps, 14.93 tbr, 90k tbn, 30k tbc
    Stream #0.1(und): Audio: aac, 16000 Hz, mono, s16, 11 kb/s
    Stream #0.2(und): Data: mp4s / 0x7334706D, 0 kb/s
    Stream #0.3(und): Data: mp4s / 0x7334706D, 0 kb/s*

This is an output from ffmpeg command line where i can get Video: part with 这是ffmpeg命令行的输出,我可以在其中获取视频:

private string ExtractVideoFormat(string rawInfo)
{
    string v = string.Empty;
    Regex re = new Regex("[V|v]ideo:.*", RegexOptions.Compiled);
    Match m = re.Match(rawInfo);
    if (m.Success)
    {
        v = m.Value;
    }
    return v;
}

and result is 结果是

mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb mpeg4,yuv420p,352x276 [PAR 1:1 DAR 88:69],344 kb

What i am trying to do is to somehow split that line and get 我想做的就是以某种方式拆分该行并获得

mpeg4
yuv420p
352x276 [PAR 1:1 DAR 88:69]
344 kb

assigned to different string objects instead of single 分配给不同的字符串对象而不是单个

String[] words = result.Split(", " , StringSplitOptions.None)

Will give you the following words in the array (I'm putting them on new lines just to make it clearer what is returned) 将在数组中给您以下单词(我将它们放在换行符上,只是为了使返回的内容更清楚)

mpeg4 MPEG4

yuv420p YUV420P

352x276 352x276

[PAR 1:1 DAR 88:69] [PAR 1:1 DAR 88:69]

344 kb 344 KB

I find using a regex application such as RegexBuddy is a very helpful visual tool when developing and debugging Regex's: 我发现在开发和调试Regex时使用正则表达式应用程序(例如RegexBuddy)是非常有用的可视化工具:

http://www.regexbuddy.com/ http://www.regexbuddy.com/

You can split the string using String.Split() . 您可以使用String.Split()拆分字符串。

string[] parts = String.Split(new [] { ", " }, text);

But when I use your expression it matches the following. 但是,当我使用您的表达式时,它与以下内容匹配。

Video: mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb /s, 14.93 fps, 14.93 tbr, 90k tbn, 30k tbc Stream #0.1(und): Audio: aac, 16000 Hz, mono, s16, 11 kb/s Stream #0.2(und): Data: mp4s / 0x7334706D, 0 kb/s Stream #0.3(und): Data: mp4s / 0x7334706D, 0 kb/s* 视频:mpeg4,yuv420p,352x276 [PAR 1:1 DAR 88:69],344 kb /s、14.93 fps,14.93 tbr,90k tbn,30k tbc流#0.1(und):音频:aac,16000 Hz,单声道, s16,11 kb / s流#0.2(und):数据:mp4s / 0x7334706D,0 kb / s流#0.3(und):数据:mp4s / 0x7334706D,0 kb / s *

This may be due to line breaks in your string. 这可能是由于字符串中的换行符。

You could use the following expression 您可以使用以下表达式

[Vv]ideo:(,? *(?<item>[^,])+)*

and capture all interesting parts in the named group item without the need to perform additional splitting. 并捕获命名组项目中所有有趣的部分,而无需执行其他拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM